Skip to content

Commit bcb1904

Browse files
committed
fix(MongoMemoryServer): rename getInstanceData() to ensureInstance(). Replace isRunning() on getInstanceInfo() sync method.
Closes #152
1 parent 5629c17 commit bcb1904

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ const dbName = await mongod.getDbName();
4040
// ... where you may use `uri` for as a connection string for mongodb or mongoose
4141

4242
// you may check instance status, after you got `uri` it must be `true`
43-
mongod.isRunning(); // return true
43+
mongod.getInstanceInfo(); // return Object with instance data
4444

4545
// you may stop mongod manually
4646
await mongod.stop();
4747

4848
// when mongod killed, it's running status should be `false`
49-
mongod.isRunning();
49+
mongod.getInstanceInfo();
5050

5151
// even you forget to stop `mongod` when you exit from script
5252
// special childProcess killer will shutdown it for you

src/MongoMemoryServer.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
SpawnOptions,
1313
} from './types';
1414
import { SynchrounousResult } from 'tmp';
15+
import { deprecate } from './util/deprecate';
1516

1617
tmp.setGracefulCleanup();
1718

@@ -43,13 +44,13 @@ const generateConnectionString = async (port: number, dbName: string): Promise<s
4344
};
4445

4546
export 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
}

src/__tests__/MongoMemoryServer-test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ describe('MongoMemoryServer', () => {
6565
});
6666
});
6767

68-
describe('getInstanceData()', () => {
68+
describe('ensureInstance()', () => {
6969
it('should throw an error if not instance is running after calling start', async () => {
7070
MongoMemoryServer.prototype.start = jest.fn(() => Promise.resolve(true));
7171

7272
const mongoServer = new MongoMemoryServer({ autoStart: false });
7373

74-
await expect(mongoServer.getInstanceData()).rejects.toThrow(
74+
await expect(mongoServer.ensureInstance()).rejects.toThrow(
7575
'Database instance is not running. You should start database by calling start() method. BTW it should start automatically if opts.autoStart!=false. Also you may provide opts.debug=true for more info.'
7676
);
7777

@@ -82,15 +82,22 @@ describe('MongoMemoryServer', () => {
8282
describe('stop()', () => {
8383
it('should stop mongod and answer on isRunning() method', async () => {
8484
const mongod = new MongoMemoryServer({
85-
autoStart: true,
85+
autoStart: false,
8686
debug: false,
8787
});
8888

89-
await mongod.getInstanceData();
89+
expect(mongod.getInstanceInfo()).toBeFalsy();
90+
mongod.start();
91+
// while mongod launching `getInstanceInfo` is false
92+
expect(mongod.getInstanceInfo()).toBeFalsy();
93+
94+
// when instance launched then data became avaliable
95+
await mongod.ensureInstance();
96+
expect(mongod.getInstanceInfo()).toBeDefined();
9097

91-
expect(mongod.isRunning()).toBeTruthy();
98+
// after stop, instance data should be empty
9299
await mongod.stop();
93-
expect(mongod.isRunning()).toBeFalsy();
100+
expect(mongod.getInstanceInfo()).toBeFalsy();
94101
});
95102
});
96103
});

src/util/deprecate.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function deprecate(msg: string): void {
2+
let stack;
3+
let stackStr = '';
4+
const error = new Error();
5+
6+
if (error.stack) {
7+
stack = error.stack.replace(/^\s+at\s+/gm, '').split('\n');
8+
stackStr = `\n ${stack.slice(2, 7).join('\n ')}`;
9+
}
10+
11+
// eslint-disable-next-line
12+
console.log(`[mongodb-memory-server]: DEPRECATION MESSAGE: ${msg} ${stackStr}\n\n`);
13+
}

0 commit comments

Comments
 (0)