Skip to content

Commit

Permalink
fix(MongoMemoryServer): pass-through option "launchTimeout" correctly
Browse files Browse the repository at this point in the history
re #710
  • Loading branch information
hasezoey committed Oct 31, 2022
1 parent 85f7e5e commit 6ffe62a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Expand Up @@ -72,6 +72,7 @@ export interface AutomaticAuth {
keyfileContent?: string;
}

// TODO: consider some way to not forget to add changes from "MongoMemoryInstanceOpts"
/**
* Data used by _startUpInstance's "data" variable
*/
Expand All @@ -84,6 +85,7 @@ export interface StartupInstanceData {
replSet?: NonNullable<MongoMemoryInstanceOpts['replSet']>;
tmpDir?: tmp.DirResult;
keyfileLocation?: NonNullable<MongoMemoryInstanceOpts['keyfileLocation']>;
launchTimeout?: NonNullable<MongoMemoryInstanceOpts['launchTimeout']>;
}

/**
Expand Down Expand Up @@ -364,6 +366,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
port = await this.getNewPort(port);
}

// consider directly using "this.opts.instance", to pass through all options, even if not defined in "StartupInstanceData"
const data: StartupInstanceData = {
port: port,
dbName: generateDbName(instOpts.dbName),
Expand All @@ -373,6 +376,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
dbPath: instOpts.dbPath,
tmpDir: undefined,
keyfileLocation: instOpts.keyfileLocation,
launchTimeout: instOpts.launchTimeout,
};

if (isNullOrUndefined(this._instanceInfo)) {
Expand Down
Expand Up @@ -1106,4 +1106,25 @@ describe('MongoMemoryServer', () => {
}
});
});

it('should transfer "launchTimeout" option to the MongoInstance', async () => {
const createSpy = jest.spyOn(MongoInstance, 'create').mockImplementation(
// @ts-expect-error This can work, because the instance is not used in the function that is tested here, beyond setting some extra options
() => Promise.resolve({})
);

const mongoServer = new MongoMemoryServer({ instance: { launchTimeout: 2000 } });

await mongoServer._startUpInstance();

// @ts-expect-error "_instanceInfo" is protected
const instanceInfo = mongoServer._instanceInfo;
expect(instanceInfo).toBeDefined();
utils.assertion(!utils.isNullOrUndefined(instanceInfo));
expect(instanceInfo.instance).toBeDefined();
expect(instanceInfo?.launchTimeout).toStrictEqual(2000);

expect(createSpy.mock.calls.length).toStrictEqual(1);
expect(createSpy.mock.calls[0][0].instance).toHaveProperty('launchTimeout', 2000);
});
});

0 comments on commit 6ffe62a

Please sign in to comment.