@@ -22,35 +22,84 @@ describe('with 5 existing servers', function () {
2222 helper . stopServers ( servers , done ) ;
2323 } ) ;
2424
25- test ( 'the getPorts() method with an argument of 3 should respond with the first three available ports (32773, 32774, 32775)' , function ( done ) {
26- portfinder . getPorts ( 3 , function ( err , ports ) {
27- expect ( err ) . toBeNull ( ) ;
28- expect ( ports ) . toEqual ( [ 32773 , 32774 , 32775 ] ) ;
29- done ( ) ;
25+ describe . each ( [
26+ [ 'getPorts()' , false , portfinder . getPorts ] ,
27+ [ 'getPorts()' , true , portfinder . getPorts ] ,
28+ [ 'getPortsPromise()' , true , portfinder . getPortsPromise ] ,
29+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
30+ test ( 'with an argument of 3 should respond with the first three available ports (32773, 32774, 32775)' , function ( done ) {
31+ if ( isPromise ) {
32+ method ( 3 )
33+ . then ( function ( ports ) {
34+ expect ( ports ) . toEqual ( [ 32773 , 32774 , 32775 ] ) ;
35+ done ( ) ;
36+ } )
37+ . catch ( function ( err ) {
38+ done ( err ) ;
39+ } ) ;
40+ } else {
41+ method ( 3 , function ( err , ports ) {
42+ if ( err ) {
43+ done ( err ) ;
44+ return ;
45+ }
46+ expect ( err ) . toBeNull ( ) ;
47+ expect ( ports ) . toEqual ( [ 32773 , 32774 , 32775 ] ) ;
48+ done ( ) ;
49+ } ) ;
50+ }
51+ } ) ;
52+
53+ test ( 'with stopPort smaller than 3 available ports' , function ( done ) {
54+ if ( isPromise ) {
55+ method ( 3 , { stopPort : 32774 } )
56+ . then ( function ( ) {
57+ done ( 'Expected error to be thrown' ) ;
58+ } )
59+ . catch ( function ( err ) {
60+ expect ( err ) . not . toBeNull ( ) ;
61+ expect ( err . message ) . toEqual ( 'No open ports found in between 32768 and 32774' ) ;
62+ done ( ) ;
63+ } ) ;
64+ } else {
65+ method ( 3 , { stopPort : 32774 } , function ( err , ports ) {
66+ expect ( err ) . not . toBeNull ( ) ;
67+ expect ( err . message ) . toEqual ( 'No open ports found in between 32768 and 32774' ) ;
68+ expect ( ports ) . toEqual ( [ 32773 , 32774 , undefined ] ) ;
69+ done ( ) ;
70+ } ) ;
71+ }
3072 } ) ;
3173 } ) ;
3274} ) ;
3375
3476describe ( 'with no existing servers' , function ( ) {
35- test ( 'the getPorts() method with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)' , function ( done ) {
36- portfinder . getPorts ( 3 , function ( err , ports ) {
37- expect ( err ) . toBeNull ( ) ;
38- expect ( ports ) . toEqual ( [ 32768 , 32769 , 32770 ] ) ;
39- done ( ) ;
77+ describe . each ( [
78+ [ 'getPorts()' , false , portfinder . getPorts ] ,
79+ [ 'getPorts()' , true , portfinder . getPorts ] ,
80+ [ 'getPortsPromise()' , true , portfinder . getPortsPromise ] ,
81+ ] ) ( `the %s method (promise: %p)` , function ( name , isPromise , method ) {
82+ test ( 'with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)' , function ( done ) {
83+ if ( isPromise ) {
84+ method ( 3 )
85+ . then ( function ( ports ) {
86+ expect ( ports ) . toEqual ( [ 32768 , 32769 , 32770 ] ) ;
87+ done ( ) ;
88+ } )
89+ . catch ( function ( err ) {
90+ done ( err ) ;
91+ } ) ;
92+ } else {
93+ method ( 3 , function ( err , ports ) {
94+ if ( err ) {
95+ done ( err ) ;
96+ return ;
97+ }
98+ expect ( err ) . toBeNull ( ) ;
99+ expect ( ports ) . toEqual ( [ 32768 , 32769 , 32770 ] ) ;
100+ done ( ) ;
101+ } ) ;
102+ }
40103 } ) ;
41104 } ) ;
42-
43- test . each ( [
44- [ 'getPorts()' , portfinder . getPorts ] ,
45- [ 'getPortsPromise()' , portfinder . getPortsPromise ] ,
46- ] ) ( 'the %s promise method with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)' , function ( name , method , done ) {
47- method ( 3 )
48- . then ( function ( ports ) {
49- expect ( ports ) . toEqual ( [ 32768 , 32769 , 32770 ] ) ;
50- done ( ) ;
51- } )
52- . catch ( function ( err ) {
53- done ( err ) ;
54- } ) ;
55- } ) ;
56105} ) ;
0 commit comments