Skip to content

Commit 778c7bb

Browse files
committed
test(MongoMemoryServer): add tests for "getStartOptions"
1 parent 6b8845e commit 778c7bb

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import * as utils from '../util/utils';
1111
import * as semver from 'semver';
1212
import { EnsureInstanceError, StateError } from '../util/errors';
1313
import { assertIsError } from './testUtils/test_utils';
14+
import { promises as fspromises } from 'fs';
15+
import * as path from 'path';
1416

1517
tmp.setGracefulCleanup();
1618
jest.setTimeout(100000); // 10s
@@ -609,6 +611,76 @@ describe('MongoMemoryServer', () => {
609611
});
610612
});
611613

614+
describe('getStartOptions()', () => {
615+
it('should create a tmpdir if "dbPath" is not set', async () => {
616+
// somehow, jest cannot redefine function "dirSync", so this is disabled
617+
// const tmpSpy = jest.spyOn(tmp, 'dirSync');
618+
const mongoServer = new MongoMemoryServer({});
619+
620+
// @ts-expect-error "getStartOptions" is protected
621+
const options = await mongoServer.getStartOptions();
622+
623+
// see comment above
624+
// expect(tmpSpy).toHaveBeenCalledTimes(1);
625+
expect(options.data.tmpDir).toBeDefined();
626+
// jest "expect" do not act as typescript typeguards
627+
utils.assertion(
628+
!utils.isNullOrUndefined(options.data.dbPath),
629+
new Error('Expected "options.data.dbPath" to be defined')
630+
);
631+
expect(await utils.pathExists(options.data.dbPath)).toEqual(true);
632+
});
633+
634+
it('should resolve "isNew" to "true" and set "createAuth" to "true" when dbPath is set, but empty', async () => {
635+
const readdirSpy = jest.spyOn(fspromises, 'readdir');
636+
const tmpDbPath = tmp.dirSync({ prefix: 'mongo-mem-getStartOptions1-', unsafeCleanup: true });
637+
638+
const mongoServer = new MongoMemoryServer({
639+
instance: { dbPath: tmpDbPath.name },
640+
auth: {},
641+
});
642+
643+
// @ts-expect-error "getStartOptions" is protected
644+
const options = await mongoServer.getStartOptions();
645+
646+
expect(options.data.tmpDir).toBeUndefined();
647+
utils.assertion(
648+
!utils.isNullOrUndefined(options.data.dbPath),
649+
new Error('Expected "options.data.dbPath" to be defined')
650+
);
651+
expect(await utils.pathExists(options.data.dbPath)).toEqual(true);
652+
expect(options.data.dbPath).toEqual(tmpDbPath.name);
653+
expect(readdirSpy).toHaveBeenCalledTimes(1);
654+
expect(options.createAuth).toEqual(true);
655+
});
656+
657+
it('should resolve "isNew" to "false" and set "createAuth" to "false" when dbPath is set, but not empty', async () => {
658+
const readdirSpy = jest.spyOn(fspromises, 'readdir');
659+
const tmpDbPath = tmp.dirSync({ prefix: 'mongo-mem-getStartOptions1-', unsafeCleanup: true });
660+
661+
// create dummy file, to make the directory non-empty
662+
await fspromises.writeFile(path.resolve(tmpDbPath.name, 'testfile'), '');
663+
664+
const mongoServer = new MongoMemoryServer({
665+
instance: { dbPath: tmpDbPath.name },
666+
auth: {},
667+
});
668+
669+
// @ts-expect-error "getStartOptions" is protected
670+
const options = await mongoServer.getStartOptions();
671+
672+
expect(options.data.tmpDir).toBeUndefined();
673+
utils.assertion(
674+
!utils.isNullOrUndefined(options.data.dbPath),
675+
new Error('Expected "options.data.dbPath" to be defined')
676+
);
677+
expect(await utils.pathExists(options.data.dbPath)).toEqual(true);
678+
expect(options.data.dbPath).toEqual(tmpDbPath.name);
679+
expect(readdirSpy).toHaveBeenCalledTimes(1);
680+
expect(options.createAuth).toEqual(false);
681+
});
682+
});
683+
612684
it('"getDbPath" should return the dbPath', async () => {
613685
const tmpDir = tmp.dirSync({ prefix: 'mongo-mem-getDbPath-', unsafeCleanup: true });
614686
const mongoServer = new MongoMemoryServer({

0 commit comments

Comments
 (0)