@@ -28,6 +28,7 @@ export interface MongoInstanceDataT {
2828 port : number ;
2929 dbPath : string ;
3030 dbName : string ;
31+ ip : string ;
3132 uri : string ;
3233 storageEngine : StorageEngineT ;
3334 instance : MongoInstance ;
@@ -39,10 +40,6 @@ export interface MongoInstanceDataT {
3940 replSet ?: string ;
4041}
4142
42- const generateConnectionString = async ( port : number , dbName : string ) : Promise < string > => {
43- return `mongodb://127.0.0.1:${ port } /${ dbName } ` ;
44- } ;
45-
4643export default class MongoMemoryServer {
4744 runningInstance : Promise < MongoInstanceDataT > | null = null ;
4845 instanceInfoSync : MongoInstanceDataT | null = null ;
@@ -102,13 +99,14 @@ export default class MongoMemoryServer {
10299 const data : any = { } ;
103100 let tmpDir : DirResult ;
104101
105- const instOpts = this . opts . instance ;
106- data . port = await getPort ( { port : ( instOpts && instOpts . port ) || undefined } ) ;
107- data . dbName = generateDbName ( instOpts && instOpts . dbName ) ;
108- data . uri = await generateConnectionString ( data . port , data . dbName ) ;
109- data . storageEngine = ( instOpts && instOpts . storageEngine ) || 'ephemeralForTest' ;
110- data . replSet = instOpts && instOpts . replSet ;
111- if ( instOpts && instOpts . dbPath ) {
102+ const instOpts = this . opts . instance || { } ;
103+ data . port = await getPort ( { port : instOpts . port || undefined } ) ;
104+ data . dbName = generateDbName ( instOpts . dbName ) ;
105+ data . ip = instOpts . ip || '127.0.0.1' ;
106+ data . uri = await this . _getUriBase ( data . ip , data . port , data . dbName ) ;
107+ data . storageEngine = instOpts . storageEngine || 'ephemeralForTest' ;
108+ data . replSet = instOpts . replSet ;
109+ if ( instOpts . dbPath ) {
112110 data . dbPath = instOpts . dbPath ;
113111 } else {
114112 tmpDir = tmp . dirSync ( {
@@ -127,13 +125,13 @@ export default class MongoMemoryServer {
127125 const instance = await MongoInstance . run ( {
128126 instance : {
129127 dbPath : data . dbPath ,
130- debug : this . opts . instance && this . opts . instance . debug ,
128+ ip : data . ip ,
131129 port : data . port ,
132130 storageEngine : data . storageEngine ,
133131 replSet : data . replSet ,
134- args : this . opts . instance && this . opts . instance . args ,
135- auth : this . opts . instance && this . opts . instance . auth ,
136- ip : this . opts . instance && this . opts . instance . ip ,
132+ debug : instOpts . debug ,
133+ args : instOpts . args ,
134+ auth : instOpts . auth ,
137135 } ,
138136 binary : this . opts . binary ,
139137 spawn : this . opts . spawn ,
@@ -183,17 +181,21 @@ export default class MongoMemoryServer {
183181 ) ;
184182 }
185183
184+ _getUriBase ( host : string , port : number , dbName : string ) {
185+ return `mongodb://${ host } :${ port } /${ dbName } ?` ;
186+ }
187+
186188 async getUri ( otherDbName : string | boolean = false ) : Promise < string > {
187- const { uri, port } : MongoInstanceDataT = await this . ensureInstance ( ) ;
189+ const { uri, port, ip } : MongoInstanceDataT = await this . ensureInstance ( ) ;
188190
189191 // IF true OR string
190192 if ( otherDbName ) {
191193 if ( typeof otherDbName === 'string' ) {
192194 // generate uri with provided DB name on existed DB instance
193- return generateConnectionString ( port , otherDbName ) ;
195+ return this . _getUriBase ( ip , port , otherDbName ) ;
194196 }
195197 // generate new random db name
196- return generateConnectionString ( port , generateDbName ( ) ) ;
198+ return this . _getUriBase ( ip , port , generateDbName ( ) ) ;
197199 }
198200
199201 return uri ;
0 commit comments