Skip to content

Commit 4ef80d5

Browse files
committed
fix(debug): improve debug messages for kill() command
Related #151
1 parent d5b9e01 commit 4ef80d5

File tree

3 files changed

+58
-33
lines changed

3 files changed

+58
-33
lines changed

src/MongoMemoryServer.ts

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

6565
async start(): Promise<boolean> {
66+
this.debug('Called MongoMemoryServer.start() method:');
6667
if (this.runningInstance) {
6768
throw new Error(
6869
'MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.'
@@ -142,6 +143,8 @@ export default class MongoMemoryServer {
142143
}
143144

144145
async stop(): Promise<boolean> {
146+
this.debug('Called MongoMemoryServer.stop() method');
147+
145148
const { instance, port, tmpDir }: MongoInstanceDataT = await this.getInstanceData();
146149

147150
this.debug(`Shutdown MongoDB server on port ${port} with pid ${instance.getPid() || ''}`);
@@ -157,8 +160,11 @@ export default class MongoMemoryServer {
157160
}
158161

159162
async getInstanceData(): Promise<MongoInstanceDataT> {
163+
this.debug('Called MongoMemoryServer.getInstanceData() method:');
160164
if (!this.runningInstance) {
165+
this.debug(' - no running instance, call `start()` command');
161166
await this.start();
167+
this.debug(' - `start()` command was succesfully resolved');
162168
}
163169
if (this.runningInstance) {
164170
return this.runningInstance;
Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
import MongoMemoryServer from '../MongoMemoryServer';
1+
import MongoMemoryServerType from '../MongoMemoryServer';
22

33
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
44

55
describe('MongoMemoryServer', () => {
6-
describe('start', () => {
7-
const mockStartUpInstance = jest.fn();
8-
9-
afterEach(() => {
10-
mockStartUpInstance.mockClear();
11-
});
6+
let MongoMemoryServer: typeof MongoMemoryServerType;
7+
beforeEach(() => {
8+
jest.resetModules();
9+
MongoMemoryServer = jest.requireActual('../MongoMemoryServer').default;
10+
});
1211

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

1716
const mongoServer = new MongoMemoryServer({ autoStart: false });
1817

19-
expect(mockStartUpInstance).toHaveBeenCalledTimes(0);
18+
expect(MongoMemoryServer.prototype._startUpInstance).toHaveBeenCalledTimes(0);
2019

2120
await expect(mongoServer.start()).resolves.toEqual(true);
2221

23-
expect(mockStartUpInstance).toHaveBeenCalledTimes(1);
22+
expect(MongoMemoryServer.prototype._startUpInstance).toHaveBeenCalledTimes(1);
2423
});
2524

2625
it('_startUpInstance should be called a second time if an error is thrown on the first call and assign the current port to nulll', async () => {
27-
mockStartUpInstance
26+
MongoMemoryServer.prototype._startUpInstance = jest
27+
.fn()
2828
.mockRejectedValueOnce(new Error('Mongod shutting down'))
2929
.mockResolvedValueOnce({});
30-
MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance;
3130

3231
const mongoServer = new MongoMemoryServer({
3332
autoStart: false,
@@ -36,16 +35,17 @@ describe('MongoMemoryServer', () => {
3635
},
3736
});
3837

39-
expect(mockStartUpInstance).toHaveBeenCalledTimes(0);
38+
expect(MongoMemoryServer.prototype._startUpInstance).toHaveBeenCalledTimes(0);
4039

4140
await expect(mongoServer.start()).resolves.toEqual(true);
4241

43-
expect(mockStartUpInstance).toHaveBeenCalledTimes(2);
42+
expect(MongoMemoryServer.prototype._startUpInstance).toHaveBeenCalledTimes(2);
4443
});
4544

4645
it('should throw an error if _startUpInstance throws an unknown error', async () => {
47-
mockStartUpInstance.mockRejectedValueOnce(new Error('unknown error'));
48-
MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance;
46+
MongoMemoryServer.prototype._startUpInstance = jest
47+
.fn()
48+
.mockRejectedValueOnce(new Error('unknown error'));
4949

5050
const mongoServer = new MongoMemoryServer({
5151
autoStart: false,
@@ -54,34 +54,43 @@ describe('MongoMemoryServer', () => {
5454
},
5555
});
5656

57-
expect(mockStartUpInstance).toHaveBeenCalledTimes(0);
57+
expect(MongoMemoryServer.prototype._startUpInstance).toHaveBeenCalledTimes(0);
5858

5959
await expect(mongoServer.start()).rejects.toThrow(
6060
`unknown error\n\nUse debug option for more info: ` +
6161
`new MongoMemoryServer({ debug: true })`
6262
);
6363

64-
expect(mockStartUpInstance).toHaveBeenCalledTimes(1);
64+
expect(MongoMemoryServer.prototype._startUpInstance).toHaveBeenCalledTimes(1);
6565
});
6666
});
67-
describe('getInstanceData', () => {
68-
const mockStart = jest.fn();
69-
70-
afterEach(() => {
71-
mockStart.mockClear();
72-
});
7367

68+
describe('getInstanceData', () => {
7469
it('should throw an error if not instance is running after calling start', async () => {
75-
mockStart.mockResolvedValue(true);
76-
MongoMemoryServer.prototype.start = mockStart;
70+
MongoMemoryServer.prototype.start = jest.fn(() => Promise.resolve(true));
7771

7872
const mongoServer = new MongoMemoryServer({ autoStart: false });
7973

8074
await expect(mongoServer.getInstanceData()).rejects.toThrow(
8175
'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.'
8276
);
8377

84-
expect(mockStart).toHaveBeenCalledTimes(1);
78+
expect(MongoMemoryServer.prototype.start).toHaveBeenCalledTimes(1);
79+
});
80+
});
81+
82+
describe('stop', () => {
83+
it.only('should stop mongod', async () => {
84+
console.log(MongoMemoryServer.prototype._startUpInstance);
85+
const mongod = new MongoMemoryServer({
86+
autoStart: true,
87+
debug: true,
88+
});
89+
90+
await mongod.getInstanceData();
91+
92+
await mongod.stop();
93+
expect(mongod.runningInstance).toBe(null);
8594
});
8695
});
8796
});

src/util/MongoInstance.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* @flow */
2-
/* eslint-disable class-methods-use-this */
3-
41
import { ChildProcess, spawn as spawnChild } from 'child_process';
52
import path from 'path';
63
import MongoBinary from './MongoBinary';
@@ -119,21 +116,34 @@ export default class MongoInstance {
119116
}
120117

121118
async kill(): Promise<MongoInstance> {
119+
this.debug('Called MongoInstance.kill():');
122120
if (this.childProcess && !this.childProcess.killed) {
123121
await new Promise((resolve) => {
124122
if (this.childProcess) {
125-
this.childProcess.once(`exit`, resolve);
123+
this.childProcess.once(`exit`, () => {
124+
this.debug(' - childProcess: got exit signal. Ok!');
125+
resolve();
126+
});
126127
this.childProcess.kill();
128+
this.debug(' - childProcess: send kill cmd...');
127129
}
128130
});
131+
} else {
132+
this.debug(' - childProcess: nothing to kill, skipping.');
129133
}
130134
if (this.killerProcess && !this.killerProcess.killed) {
131135
await new Promise((resolve) => {
132136
if (this.killerProcess) {
133-
this.killerProcess.once(`exit`, resolve);
137+
this.killerProcess.once(`exit`, () => {
138+
this.debug(' - killerProcess: got exit signal. Ok!');
139+
resolve();
140+
});
134141
this.killerProcess.kill();
142+
this.debug(' - killerProcess: send kill cmd...');
135143
}
136144
});
145+
} else {
146+
this.debug(' - killerProcess: nothing to kill, skipping.');
137147
}
138148
return this;
139149
}

0 commit comments

Comments
 (0)