Skip to content

Commit b8e4d23

Browse files
committed
feat(MongoMemoryServer): add isRunning() method
Closes #152
1 parent c2e5ab5 commit b8e4d23

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,19 @@ const dbPath = await mongod.getDbPath();
3737
const dbName = await mongod.getDbName();
3838

3939
// some code
40+
// ... where you may use `uri` for as a connection string for mongodb or mongoose
41+
42+
// you may check instance status, after you got `uri` it must be `true`
43+
mongod.isRunning(); // return true
4044

4145
// you may stop mongod manually
42-
mongod.stop();
43-
// or it will be stopped automatically when you exit from script
46+
await mongod.stop();
47+
48+
// when mongod killed, it's running status should be `false`
49+
mongod.isRunning();
50+
51+
// even you forget to stop `mongod` when you exit from script
52+
// special childProcess killer will shutdown it for you
4453
```
4554

4655
### Available options

src/MongoMemoryServer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export default class MongoMemoryServer {
6262
}
6363
}
6464

65+
isRunning(): boolean {
66+
return !!this.runningInstance;
67+
}
68+
6569
async start(): Promise<boolean> {
6670
this.debug('Called MongoMemoryServer.start() method:');
6771
if (this.runningInstance) {

src/__tests__/MongoMemoryServer-test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('MongoMemoryServer', () => {
99
MongoMemoryServer = jest.requireActual('../MongoMemoryServer').default;
1010
});
1111

12-
describe('start', () => {
12+
describe('start()', () => {
1313
it('should resolve to true if an MongoInstanceData is resolved by _startUpInstance', async () => {
1414
MongoMemoryServer.prototype._startUpInstance = jest.fn(() => Promise.resolve({} as any));
1515

@@ -65,7 +65,7 @@ describe('MongoMemoryServer', () => {
6565
});
6666
});
6767

68-
describe('getInstanceData', () => {
68+
describe('getInstanceData()', () => {
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

@@ -79,18 +79,18 @@ describe('MongoMemoryServer', () => {
7979
});
8080
});
8181

82-
describe('stop', () => {
83-
it.only('should stop mongod', async () => {
84-
console.log(MongoMemoryServer.prototype._startUpInstance);
82+
describe('stop()', () => {
83+
it('should stop mongod and answer on isRunning() method', async () => {
8584
const mongod = new MongoMemoryServer({
8685
autoStart: true,
87-
debug: true,
86+
debug: false,
8887
});
8988

9089
await mongod.getInstanceData();
9190

91+
expect(mongod.isRunning()).toBeTruthy();
9292
await mongod.stop();
93-
expect(mongod.runningInstance).toBe(null);
93+
expect(mongod.isRunning()).toBeFalsy();
9494
});
9595
});
9696
});

0 commit comments

Comments
 (0)