Skip to content

Commit c362ab7

Browse files
committed
tests: add unit test suite for MongoMemoryServer
1 parent 59a7ed6 commit c362ab7

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import MongoMemoryServer from '../MongoMemoryServer';
2+
3+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
4+
5+
describe('MongoMemoryServer', () => {
6+
describe('start', () => {
7+
const mockStartUpInstance = jest.fn();
8+
9+
afterEach(() => {
10+
mockStartUpInstance.mockClear();
11+
});
12+
13+
it('should resolve to true if an MongoInstanceData is resolved by _startUpInstance', async () => {
14+
mockStartUpInstance.mockResolvedValue({});
15+
MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance;
16+
17+
const mongoServer = new MongoMemoryServer({ autoStart: false });
18+
19+
expect(mockStartUpInstance).toHaveBeenCalledTimes(0);
20+
21+
await expect(mongoServer.start()).resolves.toEqual(true);
22+
23+
expect(mockStartUpInstance).toHaveBeenCalledTimes(1);
24+
});
25+
26+
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
28+
.mockRejectedValueOnce(new Error('Mongod shutting down'))
29+
.mockResolvedValueOnce({});
30+
MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance;
31+
32+
const mongoServer = new MongoMemoryServer({
33+
autoStart: false,
34+
instance: {
35+
port: 123,
36+
},
37+
});
38+
39+
expect(mockStartUpInstance).toHaveBeenCalledTimes(0);
40+
41+
await expect(mongoServer.start()).resolves.toEqual(true);
42+
43+
expect(mockStartUpInstance).toHaveBeenCalledTimes(2);
44+
});
45+
46+
it('should throw an error if _startUpInstance throws an unknown error', async () => {
47+
mockStartUpInstance.mockRejectedValueOnce(new Error('unknown error'));
48+
MongoMemoryServer.prototype._startUpInstance = mockStartUpInstance;
49+
50+
const mongoServer = new MongoMemoryServer({
51+
autoStart: false,
52+
instance: {
53+
port: 123,
54+
},
55+
});
56+
57+
expect(mockStartUpInstance).toHaveBeenCalledTimes(0);
58+
59+
await expect(mongoServer.start()).rejects.toThrow(
60+
`unknown error\n\nUse debug option for more info: ` +
61+
`new MongoMemoryServer({ debug: true })`
62+
);
63+
64+
expect(mockStartUpInstance).toHaveBeenCalledTimes(1);
65+
});
66+
});
67+
describe('getInstanceData', () => {
68+
const mockStart = jest.fn();
69+
70+
afterEach(() => {
71+
mockStart.mockClear();
72+
});
73+
74+
it('should throw an error if not instance is running after calling start', async () => {
75+
mockStart.mockResolvedValue(true);
76+
MongoMemoryServer.prototype.start = mockStart;
77+
78+
const mongoServer = new MongoMemoryServer({ autoStart: false });
79+
80+
await expect(mongoServer.getInstanceData()).rejects.toThrow(
81+
'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.'
82+
);
83+
84+
expect(mockStart).toHaveBeenCalledTimes(1);
85+
});
86+
});
87+
});

src/__tests__/singleDB-test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Db, MongoClient } from 'mongodb';
2-
import MongoMemoryServer from '../MongoMemoryServer';
2+
import MongoMemoryServer, { MongoInstanceDataT } from '../MongoMemoryServer';
33

44
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
5-
65
let con: MongoClient;
76
let db: Db;
87
let mongoServer: MongoMemoryServer;
@@ -27,4 +26,19 @@ describe('Single mongoServer', () => {
2726
expect(result.result).toMatchSnapshot();
2827
expect(await col.countDocuments({})).toBe(2);
2928
});
29+
30+
it('should get URI of specified DB name', async () => {
31+
const port: number = await mongoServer.getPort();
32+
expect(await mongoServer.getUri('dumb')).toBe(`mongodb://127.0.0.1:${port}/dumb`);
33+
});
34+
35+
it('should throw error on start if there is already a running instance', async () => {
36+
const mongoServer2 = new MongoMemoryServer({ autoStart: false });
37+
38+
mongoServer2.runningInstance = Promise.resolve({}) as Promise<MongoInstanceDataT>;
39+
40+
await expect(mongoServer2.start()).rejects.toThrow(
41+
'MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.'
42+
);
43+
});
3044
});

src/util/__tests__/MongoBinaryDownload-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ the same as in the reference result`, () => {
8686
);
8787
});
8888

89-
// TODO : weird test fail error TypeError: jasmine.Spec.isPendingSpecException is not a function
9089
it('false value of checkMD5 attribute disables makeMD5check validation', async () => {
9190
expect.assertions(1);
9291
(fs.readFileSync as jest.Mock).mockImplementationOnce(() => 'someMd5 fileName');

0 commit comments

Comments
 (0)