Skip to content

Commit

Permalink
fix(MongoMemoryServer::getStartOptions): use "forceSamePort", even wh…
Browse files Browse the repository at this point in the history
…en instance is not defined

fixes #578
  • Loading branch information
hasezoey committed Nov 25, 2021
1 parent fb50ae0 commit 1170ad5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,27 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
/**
* Construct Instance Starting Options
*/
protected async getStartOptions(): Promise<MongoMemoryServerGetStartOptions> {
this.debug('getStartOptions');
protected async getStartOptions(
forceSamePort: boolean = false
): Promise<MongoMemoryServerGetStartOptions> {
this.debug(`getStartOptions: forceSamePort: ${forceSamePort}`);
/** Shortcut to this.opts.instance */
const instOpts = this.opts.instance ?? {};
/**
* This variable is used for determining if "createAuth" should be run
*/
let isNew: boolean = true;

// use pre-defined port if available, otherwise generate a new port
let port = typeof instOpts.port === 'number' ? instOpts.port : undefined;

// if "forceSamePort" is not true, and get a available port
if (!forceSamePort || isNullOrUndefined(port)) {
port = await this.getNewPort(port);
}

const data: StartupInstanceData = {
port: await this.getNewPort(instOpts.port),
port: port,
dbName: generateDbName(instOpts.dbName),
ip: instOpts.ip ?? '127.0.0.1',
storageEngine: instOpts.storageEngine ?? 'ephemeralForTest',
Expand Down Expand Up @@ -412,7 +422,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
return;
}

const { mongodOptions, createAuth, data } = await this.getStartOptions();
const { mongodOptions, createAuth, data } = await this.getStartOptions(forceSamePort);
this.debug(`_startUpInstance: Creating new MongoDB instance with options:`, mongodOptions);

const instance = await MongoInstance.create(mongodOptions);
Expand Down Expand Up @@ -473,6 +483,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
}

// assert here, otherwise typescript is not happy
// TODO: remove check, typescript does not need this anymore
assertion(
!isNullOrUndefined(this._instanceInfo.instance),
new Error('"instanceInfo.instance" is undefined!')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,54 @@ describe('MongoMemoryServer', () => {
expect(readdirSpy).toHaveBeenCalledTimes(1);
expect(options.createAuth).toEqual(false);
});

it('should generate a port when no suggestion is defined', async () => {
const mongoServer = new MongoMemoryServer();
const newPortSpy = jest
// @ts-expect-error "getNewPort" is protected
.spyOn(mongoServer, 'getNewPort')
// @ts-expect-error it somehow gets a wrong function type
.mockImplementation((port) => Promise.resolve(port ? port : 2000));

// @ts-expect-error "getStartOptions" is protected
const options = await mongoServer.getStartOptions();

expect(newPortSpy).toHaveBeenCalledTimes(1);
expect(typeof options.data.port === 'number').toBeTruthy();
});

it('should use a predefined port as a suggestion for a port', async () => {
const predefinedPort = 10000;

const mongoServer = new MongoMemoryServer({ instance: { port: predefinedPort } });
const newPortSpy = jest
// @ts-expect-error "getNewPort" is protected
.spyOn(mongoServer, 'getNewPort')
// @ts-expect-error it somehow gets a wrong function type
.mockImplementation((port) => Promise.resolve(port ? port : 2000));

// @ts-expect-error "getStartOptions" is protected
const options = await mongoServer.getStartOptions();

expect(newPortSpy).toHaveBeenCalledWith(predefinedPort);
expect(options.data.port).toStrictEqual(predefinedPort);
});

it('should use a predefined port for a port with "forceSamePort" on', async () => {
const predefinedPort = 30000;

const mongoServer = new MongoMemoryServer({ instance: { port: predefinedPort } });
const newPortSpy = jest
// @ts-expect-error "getNewPort" is protected
.spyOn(mongoServer, 'getNewPort')
.mockImplementation(() => fail('Expected this function to not be called'));

// @ts-expect-error "getStartOptions" is protected
const options = await mongoServer.getStartOptions(true);

expect(newPortSpy).not.toHaveBeenCalled();
expect(options.data.port).toStrictEqual(predefinedPort);
});
});

it('"getDbPath" should return the dbPath', async () => {
Expand Down

0 comments on commit 1170ad5

Please sign in to comment.