@@ -12,6 +12,7 @@ import {
1212 SpawnOptions ,
1313} from './types' ;
1414import { SynchrounousResult } from 'tmp' ;
15+ import { deprecate } from './util/deprecate' ;
1516
1617tmp . setGracefulCleanup ( ) ;
1718
@@ -43,13 +44,13 @@ const generateConnectionString = async (port: number, dbName: string): Promise<s
4344} ;
4445
4546export default class MongoMemoryServer {
46- runningInstance : Promise < MongoInstanceDataT > | null ;
47+ runningInstance : Promise < MongoInstanceDataT > | null = null ;
48+ instanceInfoSync : MongoInstanceDataT | null = null ;
4749 opts : MongoMemoryServerOptsT ;
4850 debug : DebugFn ;
4951
5052 constructor ( opts ?: MongoMemoryServerOptsT ) {
5153 this . opts = { ...opts } ;
52- this . runningInstance = null ;
5354
5455 this . debug = ( msg : string ) => {
5556 if ( this . opts . debug ) {
@@ -62,10 +63,6 @@ export default class MongoMemoryServer {
6263 }
6364 }
6465
65- isRunning ( ) : boolean {
66- return ! ! this . runningInstance ;
67- }
68-
6966 async start ( ) : Promise < boolean > {
7067 this . debug ( 'Called MongoMemoryServer.start() method:' ) ;
7168 if ( this . runningInstance ) {
@@ -95,7 +92,10 @@ export default class MongoMemoryServer {
9592 throw err ;
9693 } ) ;
9794
98- return this . runningInstance . then ( ( ) => true ) ;
95+ return this . runningInstance . then ( ( data ) => {
96+ this . instanceInfoSync = data ;
97+ return true ;
98+ } ) ;
9999 }
100100
101101 async _startUpInstance ( ) : Promise < MongoInstanceDataT > {
@@ -149,22 +149,36 @@ export default class MongoMemoryServer {
149149 async stop ( ) : Promise < boolean > {
150150 this . debug ( 'Called MongoMemoryServer.stop() method' ) ;
151151
152- const { instance, port, tmpDir } : MongoInstanceDataT = await this . getInstanceData ( ) ;
152+ const { instance, port, tmpDir } : MongoInstanceDataT = await this . ensureInstance ( ) ;
153153
154154 this . debug ( `Shutdown MongoDB server on port ${ port } with pid ${ instance . getPid ( ) || '' } ` ) ;
155155 await instance . kill ( ) ;
156156
157+ this . runningInstance = null ;
158+ this . instanceInfoSync = null ;
159+
157160 if ( tmpDir ) {
158161 this . debug ( `Removing tmpDir ${ tmpDir . name } ` ) ;
159162 tmpDir . removeCallback ( ) ;
160163 }
161164
162- this . runningInstance = null ;
163165 return true ;
164166 }
165167
168+ getInstanceInfo ( ) : MongoInstanceDataT | false {
169+ return this . instanceInfoSync || false ;
170+ }
171+
172+ /* @deprecated 5.0.0 */
166173 async getInstanceData ( ) : Promise < MongoInstanceDataT > {
167- this . debug ( 'Called MongoMemoryServer.getInstanceData() method:' ) ;
174+ deprecate (
175+ `method MongoMemoryServer.getInstanceData() will be deprecated. Please use 'MongoMemoryServer.ensureInstance()' method instead.`
176+ ) ;
177+ return this . ensureInstance ( ) ;
178+ }
179+
180+ async ensureInstance ( ) : Promise < MongoInstanceDataT > {
181+ this . debug ( 'Called MongoMemoryServer.ensureInstance() method:' ) ;
168182 if ( ! this . runningInstance ) {
169183 this . debug ( ' - no running instance, call `start()` command' ) ;
170184 await this . start ( ) ;
@@ -179,7 +193,7 @@ export default class MongoMemoryServer {
179193 }
180194
181195 async getUri ( otherDbName : string | boolean = false ) : Promise < string > {
182- const { uri, port } : MongoInstanceDataT = await this . getInstanceData ( ) ;
196+ const { uri, port } : MongoInstanceDataT = await this . ensureInstance ( ) ;
183197
184198 // IF true OR string
185199 if ( otherDbName ) {
@@ -199,17 +213,17 @@ export default class MongoMemoryServer {
199213 }
200214
201215 async getPort ( ) : Promise < number > {
202- const { port } : MongoInstanceDataT = await this . getInstanceData ( ) ;
216+ const { port } : MongoInstanceDataT = await this . ensureInstance ( ) ;
203217 return port ;
204218 }
205219
206220 async getDbPath ( ) : Promise < string > {
207- const { dbPath } : MongoInstanceDataT = await this . getInstanceData ( ) ;
221+ const { dbPath } : MongoInstanceDataT = await this . ensureInstance ( ) ;
208222 return dbPath ;
209223 }
210224
211225 async getDbName ( ) : Promise < string > {
212- const { dbName } : MongoInstanceDataT = await this . getInstanceData ( ) ;
226+ const { dbName } : MongoInstanceDataT = await this . ensureInstance ( ) ;
213227 return dbName ;
214228 }
215229}
0 commit comments