@@ -3,6 +3,7 @@ import * as tmp from 'tmp';
33import * as dbUtil from '../utils' ;
44import MongodbInstance , { MongoInstanceEvents } from '../MongoInstance' ;
55import resolveConfig , { ResolveConfigVariables } from '../resolveConfig' ;
6+ import getPort from 'get-port' ;
67
78jasmine . DEFAULT_TIMEOUT_INTERVAL = 600000 ;
89tmp . setGracefulCleanup ( ) ;
@@ -126,9 +127,10 @@ describe('MongodbInstance', () => {
126127 } ) ;
127128 } ) ;
128129
129- it ( 'should start instance on port 27333' , async ( ) => {
130+ it ( 'should start instance on port specific port' , async ( ) => {
131+ const gotPort = await getPort ( { port : 27333 } ) ;
130132 const mongod = await MongodbInstance . create ( {
131- instance : { port : 27333 , dbPath : tmpDir . name } ,
133+ instance : { port : gotPort , dbPath : tmpDir . name } ,
132134 binary : { version } ,
133135 } ) ;
134136
@@ -138,24 +140,26 @@ describe('MongodbInstance', () => {
138140 } ) ;
139141
140142 it ( 'should throw error if port is busy' , async ( ) => {
143+ const gotPort = await getPort ( { port : 27444 } ) ;
141144 const mongod = await MongodbInstance . create ( {
142- instance : { port : 27444 , dbPath : tmpDir . name } ,
145+ instance : { port : gotPort , dbPath : tmpDir . name } ,
143146 binary : { version } ,
144147 } ) ;
145148
146149 await expect (
147150 MongodbInstance . create ( {
148- instance : { port : 27444 , dbPath : tmpDir . name } ,
151+ instance : { port : gotPort , dbPath : tmpDir . name } ,
149152 binary : { version } ,
150153 } )
151- ) . rejects . toEqual ( ' Port 27444 already in use' ) ;
154+ ) . rejects . toEqual ( ` Port ${ gotPort } already in use` ) ;
152155
153156 await mongod . stop ( ) ;
154157 } ) ;
155158
156159 it ( 'should wait until childprocess and killerprocess are killed' , async ( ) => {
160+ const gotPort = await getPort ( { port : 27445 } ) ;
157161 const mongod : MongodbInstance = await MongodbInstance . create ( {
158- instance : { port : 27445 , dbPath : tmpDir . name } ,
162+ instance : { port : gotPort , dbPath : tmpDir . name } ,
159163 binary : { version } ,
160164 } ) ;
161165 const pid : any = mongod . childProcess ! . pid ;
@@ -171,8 +175,9 @@ describe('MongodbInstance', () => {
171175 } ) ;
172176
173177 it ( 'should work with mongodb 4.0.3' , async ( ) => {
178+ const gotPort = await getPort ( { port : 27445 } ) ;
174179 const mongod = await MongodbInstance . create ( {
175- instance : { port : 27445 , dbPath : tmpDir . name } ,
180+ instance : { port : gotPort , dbPath : tmpDir . name } ,
176181 binary : { version : '4.0.3' } ,
177182 } ) ;
178183 expect ( mongod . childProcess ! . pid ) . toBeGreaterThan ( 0 ) ;
0 commit comments