Skip to content

Commit 8c224d1

Browse files
committed
test(MongoInstance): change "closeHandler" event tests to be in a describe block
1 parent 6043bf9 commit 8c224d1

File tree

2 files changed

+74
-65
lines changed

2 files changed

+74
-65
lines changed

packages/mongodb-memory-server-core/src/util/__tests__/MongoInstance.test.ts

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -307,79 +307,88 @@ describe('MongodbInstance', () => {
307307
expect(events.get(MongoInstanceEvents.instanceSTDERR)).toEqual(['hello']);
308308
});
309309

310-
it('"closeHandler" should emit "instanceClosed"', () => {
311-
// test both code and signal
312-
{
313-
events.clear();
314-
mongod.closeHandler(0, 'SIG');
315-
316-
expect(events.size).toEqual(1);
317-
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([0, 'SIG']);
318-
}
319-
// test only code
320-
{
321-
events.clear();
322-
mongod.closeHandler(0, null);
323-
324-
expect(events.size).toEqual(1);
325-
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([0, null]);
326-
}
327-
// test only Signal
328-
{
329-
events.clear();
330-
mongod.closeHandler(null, 'SIG');
310+
describe('closeHandler()', () => {
311+
const origPlatform = process.platform;
312+
beforeEach(() => {
313+
Object.defineProperty(process, 'platform', {
314+
value: origPlatform,
315+
});
316+
});
331317

332-
expect(events.size).toEqual(2);
333-
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIG']);
318+
it('should emit "instanceClosed"', () => {
319+
// test both code and signal
320+
{
321+
events.clear();
322+
mongod.closeHandler(0, 'SIG');
334323

335-
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
336-
expect(event).toBeInstanceOf(UnexpectedCloseError);
337-
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
338-
expect(event.message).toMatchSnapshot();
339-
}
340-
});
324+
expect(events.size).toEqual(1);
325+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([0, 'SIG']);
326+
}
327+
// test only code
328+
{
329+
events.clear();
330+
mongod.closeHandler(0, null);
341331

342-
it('"closeHandler" should emit "instanceError" with extra information on "SIGILL"', () => {
343-
// test SIGILL
344-
mongod.closeHandler(null, 'SIGILL');
332+
expect(events.size).toEqual(1);
333+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([0, null]);
334+
}
335+
// test only Signal
336+
{
337+
events.clear();
338+
mongod.closeHandler(null, 'SIG');
345339

346-
expect(events.size).toEqual(2);
347-
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIGILL']);
340+
expect(events.size).toEqual(2);
341+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIG']);
348342

349-
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
350-
expect(event).toBeInstanceOf(UnexpectedCloseError);
351-
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
352-
expect(event.message).toMatchSnapshot();
353-
});
343+
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
344+
expect(event).toBeInstanceOf(UnexpectedCloseError);
345+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
346+
expect(event.message).toMatchSnapshot();
347+
}
348+
});
354349

355-
it('"closeHandler" should emit "instanceError" with non-0 or non-12 code', () => {
356-
// test code non-0
357-
{
358-
events.clear();
359-
mongod.closeHandler(1, null);
350+
it('should emit "instanceError" with extra information on "SIGILL"', () => {
351+
// test SIGILL
352+
mongod.closeHandler(null, 'SIGILL');
360353

361354
expect(events.size).toEqual(2);
362-
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([1, null]);
355+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIGILL']);
363356

364357
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
365358
expect(event).toBeInstanceOf(UnexpectedCloseError);
366359
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
367360
expect(event.message).toMatchSnapshot();
368-
}
361+
});
369362

370-
// test signal
371-
{
372-
events.clear();
373-
mongod.closeHandler(null, 'SIG');
363+
it('should emit "instanceError" with non-0 or non-12 code', () => {
364+
// test code non-0
365+
{
366+
events.clear();
367+
mongod.closeHandler(1, null);
374368

375-
expect(events.size).toEqual(2);
376-
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIG']);
369+
expect(events.size).toEqual(2);
370+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([1, null]);
377371

378-
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
379-
expect(event).toBeInstanceOf(UnexpectedCloseError);
380-
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
381-
expect(event.message).toMatchSnapshot();
382-
}
372+
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
373+
expect(event).toBeInstanceOf(UnexpectedCloseError);
374+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
375+
expect(event.message).toMatchSnapshot();
376+
}
377+
378+
// test signal
379+
{
380+
events.clear();
381+
mongod.closeHandler(null, 'SIG');
382+
383+
expect(events.size).toEqual(2);
384+
expect(events.get(MongoInstanceEvents.instanceClosed)).toEqual([null, 'SIG']);
385+
386+
const event = events.get(MongoInstanceEvents.instanceError)?.[0];
387+
expect(event).toBeInstanceOf(UnexpectedCloseError);
388+
assertIsError(event); // has to be used, because there is not typeguard from "expect(variable).toBeInstanceOf"
389+
expect(event.message).toMatchSnapshot();
390+
}
391+
});
383392
});
384393

385394
describe('stdoutHandler()', () => {

packages/mongodb-memory-server-core/src/util/__tests__/__snapshots__/MongoInstance.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ exports[`MongodbInstance prepareCommandArgs should throw an error if no dbpath i
66

77
exports[`MongodbInstance prepareCommandArgs should throw an error if no port is provided 1`] = `"\\"instanceOpts.port\\" is required to be set!"`;
88

9-
exports[`MongodbInstance test events "closeHandler" should emit "instanceClosed" 1`] = `"Instance closed unexpectedly with code \\"null\\" and signal \\"SIG\\""`;
9+
exports[`MongodbInstance test events "start" should emit a "instanceError" when timeout is reached and throw a error 1`] = `"Instance failed to start within 1000ms"`;
10+
11+
exports[`MongodbInstance test events checkErrorInLine() should emit "instanceError" when shared libraries fail to load 1`] = `"Instance failed to start because a library is missing or cannot be opened: \\"libcrypto.so.1.1\\""`;
12+
13+
exports[`MongodbInstance test events closeHandler() should emit "instanceClosed" 1`] = `"Instance closed unexpectedly with code \\"null\\" and signal \\"SIG\\""`;
1014

11-
exports[`MongodbInstance test events "closeHandler" should emit "instanceError" with extra information on "SIGILL" 1`] = `
15+
exports[`MongodbInstance test events closeHandler() should emit "instanceError" with extra information on "SIGILL" 1`] = `
1216
"Instance closed unexpectedly with code \\"null\\" and signal \\"SIGILL\\"
1317
The Process Exited with SIGILL, which mean illegal instruction, which is commonly thrown in mongodb 5.0+ when not having AVX available on the CPU"
1418
`;
1519

16-
exports[`MongodbInstance test events "closeHandler" should emit "instanceError" with non-0 or non-12 code 1`] = `"Instance closed unexpectedly with code \\"1\\" and signal \\"null\\""`;
20+
exports[`MongodbInstance test events closeHandler() should emit "instanceError" with non-0 or non-12 code 1`] = `"Instance closed unexpectedly with code \\"1\\" and signal \\"null\\""`;
1721

18-
exports[`MongodbInstance test events "closeHandler" should emit "instanceError" with non-0 or non-12 code 2`] = `"Instance closed unexpectedly with code \\"null\\" and signal \\"SIG\\""`;
19-
20-
exports[`MongodbInstance test events "start" should emit a "instanceError" when timeout is reached and throw a error 1`] = `"Instance failed to start within 1000ms"`;
21-
22-
exports[`MongodbInstance test events checkErrorInLine() should emit "instanceError" when shared libraries fail to load 1`] = `"Instance failed to start because a library is missing or cannot be opened: \\"libcrypto.so.1.1\\""`;
22+
exports[`MongodbInstance test events closeHandler() should emit "instanceError" with non-0 or non-12 code 2`] = `"Instance closed unexpectedly with code \\"null\\" and signal \\"SIG\\""`;
2323

2424
exports[`MongodbInstance test events stdoutHandler() should emit "instanceError" when "excepetion in initAndListen" is thrown DBException in initAndListen 1`] = `
2525
"Instance Failed to start with \\"DBException in initAndListen\\". Original Error:

0 commit comments

Comments
 (0)