/
findPort.test.js
130 lines (102 loc) · 3.21 KB
/
findPort.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const http = require('http');
const portfinder = require('portfinder');
const findPort = require('../../../lib/utils/findPort');
describe('findPort util', () => {
let dummyServers = [];
afterEach(() => {
delete process.env.DEFAULT_PORT_RETRY;
return dummyServers
.reduce((p, server) => {
return p.then(() => {
return new Promise((resolve) => {
server.close(resolve);
});
});
}, Promise.resolve())
.then(() => {
dummyServers = [];
});
});
function createDummyServers(n) {
return (Array.isArray(n) ? n : [...new Array(n)]).reduce((p, _, i) => {
return p.then(() => {
return new Promise((resolve) => {
const server = http.createServer();
dummyServers.push(server);
server.listen(8080 + i, resolve);
});
});
}, Promise.resolve());
}
it('should returns the port when the port is specified', () => {
process.env.DEFAULT_PORT_RETRY = 5;
return findPort(8082).then((port) => {
expect(port).toEqual(8082);
});
});
it.only('should returns the port when the port is null', () => {
const retryCount = 2;
process.env.DEFAULT_PORT_RETRY = 2;
return createDummyServers(retryCount)
.then(() => findPort(null))
.then((port) => {
expect(port).toEqual(8080 + retryCount);
});
});
it('should returns the port when the port is undefined', () => {
const retryCount = 2;
process.env.DEFAULT_PORT_RETRY = 2;
return (
createDummyServers(retryCount)
// eslint-disable-next-line no-undefined
.then(() => findPort(undefined))
.then((port) => {
expect(port).toEqual(8080 + retryCount);
})
);
});
it('should retry finding the port for up to defaultPortRetry times (number)', () => {
const retryCount = 3;
process.env.DEFAULT_PORT_RETRY = retryCount;
return createDummyServers(retryCount)
.then(() => findPort())
.then((port) => {
expect(port).toEqual(8080 + retryCount);
});
});
it('should retry finding the port for up to defaultPortRetry times (string)', () => {
const retryCount = 3;
process.env.DEFAULT_PORT_RETRY = `${retryCount}`;
return createDummyServers(retryCount)
.then(() => findPort())
.then((port) => {
expect(port).toEqual(8080 + retryCount);
});
});
it('should retry finding the port when serial ports are busy', () => {
const busyPorts = [8080, 8081, 8082, 8083];
process.env.DEFAULT_PORT_RETRY = 3;
return createDummyServers(busyPorts)
.then(() => findPort())
.then((port) => {
expect(port).toEqual(8080 + busyPorts.length);
});
});
it("should throws the error when the port isn't found", () => {
expect.assertions(1);
const spy = jest
.spyOn(portfinder, 'getPort')
.mockImplementation((callback) => {
return callback(new Error('busy'));
});
const retryCount = 1;
process.env.DEFAULT_PORT_RETRY = 0;
return createDummyServers(retryCount)
.then(() => findPort())
.catch((err) => {
expect(err.message).toMatchSnapshot();
spy.mockRestore();
});
});
});