Skip to content

Commit 401441c

Browse files
committed
fix(MongoMemoryReplSet): getConnectionString() now returns uri with ?replicaSet= option.
BREAKING CHANGE: `getConnectionString()` now returns uri with `?replicaSet=` option.
1 parent 7eb36ea commit 401441c

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export default class MongoMemoryReplSet extends EventEmitter {
156156
}
157157
const ports = await Promise.all(this.servers.map((s) => s.getPort()));
158158
const hosts = ports.map((port) => `127.0.0.1:${port}`).join(',');
159-
return `mongodb://${hosts}/${dbName}`;
159+
return `mongodb://${hosts}/${dbName}?replicaSet=${this.opts.replSet.name}`;
160160
}
161161

162162
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ describe('single server replset', () => {
1919
const uri = await replSet.getUri('other');
2020
const str = await replSet.getConnectionString('other');
2121
expect(uri.split(',').length).toEqual(1);
22-
expect(uri.endsWith('/other')).toBeTruthy();
22+
expect(uri.includes('/other')).toBeTruthy();
23+
expect(uri.includes('replicaSet=testset')).toBeTruthy();
2324
expect(str).toEqual(uri);
2425

2526
await replSet.stop();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MongoClient } from 'mongodb';
44
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
55

66
describe('multi-member replica set', () => {
7-
it.only('should enter running state', async () => {
7+
it('should enter running state', async () => {
88
const opts: any = { replSet: { count: 3 } };
99
const replSet = new MongoMemoryReplSet(opts);
1010
await replSet.waitUntilRunning();
@@ -21,11 +21,17 @@ describe('multi-member replica set', () => {
2121
await replSet.waitUntilRunning();
2222
const uri = await replSet.getUri();
2323

24-
await MongoClient.connect(`${uri}?replicaSet=testset`, {
24+
const conn = await MongoClient.connect(uri, {
2525
useNewUrlParser: true,
2626
useUnifiedTopology: true,
2727
});
2828

29+
const db = await conn.db(await replSet.getDbName());
30+
const admin = db.admin();
31+
const status = await admin.replSetGetStatus();
32+
expect(status.members.filter((m: any) => m.stateStr === 'PRIMARY')).toHaveLength(1);
33+
expect(status.members.filter((m: any) => m.stateStr === 'SECONDARY')).toHaveLength(2);
34+
2935
await replSet.stop();
3036
});
3137
});

0 commit comments

Comments
 (0)