Skip to content

Commit 505b7c2

Browse files
committed
fix(MongoMemoryServer): add ? character to the connection string (uri) by default (hotfix for v6.0.0)
It helps in future translate config options to connectionString without introducing breaking changes
1 parent a5b4190 commit 505b7c2

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface MongoInstanceDataT {
2828
port: number;
2929
dbPath: string;
3030
dbName: string;
31+
ip: string;
3132
uri: string;
3233
storageEngine: StorageEngineT;
3334
instance: MongoInstance;
@@ -39,10 +40,6 @@ export interface MongoInstanceDataT {
3940
replSet?: string;
4041
}
4142

42-
const generateConnectionString = async (port: number, dbName: string): Promise<string> => {
43-
return `mongodb://127.0.0.1:${port}/${dbName}`;
44-
};
45-
4643
export default class MongoMemoryServer {
4744
runningInstance: Promise<MongoInstanceDataT> | null = null;
4845
instanceInfoSync: MongoInstanceDataT | null = null;
@@ -102,13 +99,14 @@ export default class MongoMemoryServer {
10299
const data: any = {};
103100
let tmpDir: DirResult;
104101

105-
const instOpts = this.opts.instance;
106-
data.port = await getPort({ port: (instOpts && instOpts.port) || undefined });
107-
data.dbName = generateDbName(instOpts && instOpts.dbName);
108-
data.uri = await generateConnectionString(data.port, data.dbName);
109-
data.storageEngine = (instOpts && instOpts.storageEngine) || 'ephemeralForTest';
110-
data.replSet = instOpts && instOpts.replSet;
111-
if (instOpts && instOpts.dbPath) {
102+
const instOpts = this.opts.instance || {};
103+
data.port = await getPort({ port: instOpts.port || undefined });
104+
data.dbName = generateDbName(instOpts.dbName);
105+
data.ip = instOpts.ip || '127.0.0.1';
106+
data.uri = await this._getUriBase(data.ip, data.port, data.dbName);
107+
data.storageEngine = instOpts.storageEngine || 'ephemeralForTest';
108+
data.replSet = instOpts.replSet;
109+
if (instOpts.dbPath) {
112110
data.dbPath = instOpts.dbPath;
113111
} else {
114112
tmpDir = tmp.dirSync({
@@ -127,13 +125,13 @@ export default class MongoMemoryServer {
127125
const instance = await MongoInstance.run({
128126
instance: {
129127
dbPath: data.dbPath,
130-
debug: this.opts.instance && this.opts.instance.debug,
128+
ip: data.ip,
131129
port: data.port,
132130
storageEngine: data.storageEngine,
133131
replSet: data.replSet,
134-
args: this.opts.instance && this.opts.instance.args,
135-
auth: this.opts.instance && this.opts.instance.auth,
136-
ip: this.opts.instance && this.opts.instance.ip,
132+
debug: instOpts.debug,
133+
args: instOpts.args,
134+
auth: instOpts.auth,
137135
},
138136
binary: this.opts.binary,
139137
spawn: this.opts.spawn,
@@ -183,17 +181,21 @@ export default class MongoMemoryServer {
183181
);
184182
}
185183

184+
_getUriBase(host: string, port: number, dbName: string) {
185+
return `mongodb://${host}:${port}/${dbName}?`;
186+
}
187+
186188
async getUri(otherDbName: string | boolean = false): Promise<string> {
187-
const { uri, port }: MongoInstanceDataT = await this.ensureInstance();
189+
const { uri, port, ip }: MongoInstanceDataT = await this.ensureInstance();
188190

189191
// IF true OR string
190192
if (otherDbName) {
191193
if (typeof otherDbName === 'string') {
192194
// generate uri with provided DB name on existed DB instance
193-
return generateConnectionString(port, otherDbName);
195+
return this._getUriBase(ip, port, otherDbName);
194196
}
195197
// generate new random db name
196-
return generateConnectionString(port, generateDbName());
198+
return this._getUriBase(ip, port, generateDbName());
197199
}
198200

199201
return uri;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ describe('Multiple mongoServers', () => {
5555
it('should have different uri', async () => {
5656
const uri1 = await mongoServer1.getConnectionString();
5757
const uri2 = await mongoServer2.getConnectionString();
58-
5958
expect(uri1).not.toEqual(uri2);
6059
});
60+
61+
it('v6.0.0 adds "?" to the connection string (uri)', async () => {
62+
const uri1 = await mongoServer1.getConnectionString();
63+
expect(uri1.includes('?')).toBeTruthy();
64+
});
6165
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Single mongoServer', () => {
3333

3434
it('should get URI of specified DB name', async () => {
3535
const port: number = await mongoServer.getPort();
36-
expect(await mongoServer.getUri('dumb')).toBe(`mongodb://127.0.0.1:${port}/dumb`);
36+
expect(await mongoServer.getUri('dumb')).toBe(`mongodb://127.0.0.1:${port}/dumb?`);
3737
});
3838

3939
it('should throw error on start if there is already a running instance', async () => {

0 commit comments

Comments
 (0)