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