Skip to content

Commit 05e2595

Browse files
committed
test(MongoMemoryServer::cleanup): try to clean-up more after tests
1 parent 4881f44 commit 05e2595

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,17 +621,30 @@ describe('MongoMemoryServer', () => {
621621
});
622622

623623
describe('cleanup()', () => {
624+
/** Cleanup the created tmp-dir, even if the cleanup test tested to not clean it up */
625+
let tmpdir: tmp.DirResult | undefined;
626+
624627
// "beforeAll" dosnt work here, thanks to the top-level "afterAll" hook
625628
beforeEach(() => {
626629
jest.spyOn(utils, 'statPath');
627630
// @ts-expect-error because "default" dosnt exist in the definitions
628631
jest.spyOn(semver.default, 'lt'); // it needs to be ".default" otherwise "lt" is only an getter
629632
});
630633

634+
afterEach(() => {
635+
if (!utils.isNullOrUndefined(tmpdir)) {
636+
tmpdir.removeCallback();
637+
638+
tmpdir = undefined; // reset, just to be sure its clean
639+
}
640+
});
641+
631642
it('should properly cleanup with tmpDir with default no force (old)', async () => {
632643
const mongoServer = await MongoMemoryServer.create();
633644
const dbPath = mongoServer.instanceInfo!.dbPath;
634645

646+
tmpdir = mongoServer.instanceInfo?.tmpDir;
647+
635648
await mongoServer.stop({ doCleanup: false });
636649
await mongoServer.cleanup();
637650

@@ -646,6 +659,8 @@ describe('MongoMemoryServer', () => {
646659
const mongoServer = await MongoMemoryServer.create();
647660
const dbPath = mongoServer.instanceInfo!.dbPath;
648661

662+
tmpdir = mongoServer.instanceInfo?.tmpDir;
663+
649664
await mongoServer.stop({ doCleanup: false });
650665
await mongoServer.cleanup(true);
651666

@@ -661,6 +676,8 @@ describe('MongoMemoryServer', () => {
661676
const mongoServer = await MongoMemoryServer.create({ instance: { dbPath: tmpDir.name } });
662677
const dbPath = mongoServer.instanceInfo!.dbPath;
663678

679+
tmpdir = mongoServer.instanceInfo?.tmpDir;
680+
664681
await mongoServer.stop({ doCleanup: false });
665682
await mongoServer.cleanup(true);
666683

@@ -673,10 +690,12 @@ describe('MongoMemoryServer', () => {
673690

674691
it('should properly cleanup with tmpDir with default no force (new)', async () => {
675692
const mongoServer = await MongoMemoryServer.create();
693+
const dbPath = mongoServer.instanceInfo!.dbPath;
676694

677695
const cleanupSpy = jest.spyOn(mongoServer, 'cleanup');
678696

679-
const dbPath = mongoServer.instanceInfo!.dbPath;
697+
tmpdir = mongoServer.instanceInfo?.tmpDir;
698+
680699
await mongoServer.stop({ doCleanup: false });
681700
await mongoServer.cleanup();
682701
expect(utils.statPath).not.toHaveBeenCalled();
@@ -692,6 +711,8 @@ describe('MongoMemoryServer', () => {
692711

693712
const cleanupSpy = jest.spyOn(mongoServer, 'cleanup');
694713

714+
tmpdir = mongoServer.instanceInfo?.tmpDir;
715+
695716
const dbPath = mongoServer.instanceInfo!.dbPath;
696717
await mongoServer.stop({ doCleanup: false });
697718
await mongoServer.cleanup({ doCleanup: true, force: true });
@@ -706,10 +727,12 @@ describe('MongoMemoryServer', () => {
706727
it('should properly cleanup with force (without tmpDir) (new)', async () => {
707728
const tmpDir = tmp.dirSync({ prefix: 'mongo-mem-cleanup-', unsafeCleanup: true });
708729
const mongoServer = await MongoMemoryServer.create({ instance: { dbPath: tmpDir.name } });
730+
const dbPath = mongoServer.instanceInfo!.dbPath;
709731

710732
const cleanupSpy = jest.spyOn(mongoServer, 'cleanup');
711733

712-
const dbPath = mongoServer.instanceInfo!.dbPath;
734+
tmpdir = mongoServer.instanceInfo?.tmpDir;
735+
713736
await mongoServer.stop({ doCleanup: false });
714737
await mongoServer.cleanup({ doCleanup: true, force: true });
715738
expect(utils.statPath).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)