Skip to content

Commit d554696

Browse files
committed
test: refactor ReplSet tests, split them for better debugging
1 parent 740bff7 commit d554696

File tree

2 files changed

+66
-60
lines changed

2 files changed

+66
-60
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import MongoMemoryReplSet from '../MongoMemoryReplSet';
2+
import { MongoClient } from 'mongodb';
3+
4+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
5+
6+
describe('single server replset', () => {
7+
it('should enter running state', async () => {
8+
const replSet = new MongoMemoryReplSet();
9+
await replSet.waitUntilRunning();
10+
const uri = await replSet.getUri();
11+
expect(uri.split(',').length).toEqual(1);
12+
13+
await replSet.stop();
14+
});
15+
16+
it('should be able to get connection string to specific db', async () => {
17+
const replSet = new MongoMemoryReplSet({});
18+
await replSet.waitUntilRunning();
19+
const uri = await replSet.getUri('other');
20+
const str = await replSet.getConnectionString('other');
21+
expect(uri.split(',').length).toEqual(1);
22+
expect(uri.endsWith('/other')).toBeTruthy();
23+
expect(str).toEqual(uri);
24+
25+
await replSet.stop();
26+
});
27+
28+
it('should be able to get dbName', async () => {
29+
const opts: any = { autoStart: false, replSet: { dbName: 'static' } };
30+
const replSet = new MongoMemoryReplSet(opts);
31+
const dbName = await replSet.getDbName();
32+
expect(dbName).toEqual('static');
33+
34+
await replSet.stop();
35+
});
36+
37+
it('should not autostart if autostart: false', async () => {
38+
const replSet = new MongoMemoryReplSet({ autoStart: false });
39+
await new Promise((resolve, reject) => {
40+
replSet.once('state', (state) => reject(new Error(`Invalid state: ${state}`)));
41+
setTimeout(resolve, 500);
42+
});
43+
44+
await replSet.stop();
45+
});
46+
47+
it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
48+
const replSet = new MongoMemoryReplSet();
49+
await replSet.waitUntilRunning();
50+
const uri = await replSet.getUri();
51+
52+
await MongoClient.connect(`${uri}?replicaSet=testset`, {
53+
useNewUrlParser: true,
54+
useUnifiedTopology: true,
55+
});
56+
57+
await replSet.stop();
58+
});
59+
});

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

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,29 @@ import { MongoClient } from 'mongodb';
33

44
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
55

6-
describe('single server replset', () => {
7-
let replSet: MongoMemoryReplSet;
8-
afterEach(async () => {
9-
if (!(replSet && replSet._state === 'running')) return;
10-
await replSet.stop();
11-
});
12-
13-
it('should enter running state', async () => {
14-
replSet = new MongoMemoryReplSet();
15-
await replSet.waitUntilRunning();
16-
const uri = await replSet.getUri();
17-
expect(uri.split(',').length).toEqual(1);
18-
});
19-
20-
it('should be able to get connection string to specific db', async () => {
21-
replSet = new MongoMemoryReplSet();
22-
await replSet.waitUntilRunning();
23-
const uri = await replSet.getUri('other');
24-
const str = await replSet.getConnectionString('other');
25-
expect(uri.split(',').length).toEqual(1);
26-
expect(uri.endsWith('/other')).toBeTruthy();
27-
expect(str).toEqual(uri);
28-
});
29-
30-
it('should be able to get dbName', async () => {
31-
const opts: any = { autoStart: false, replSet: { dbName: 'static' } };
32-
replSet = new MongoMemoryReplSet(opts);
33-
const dbName = await replSet.getDbName();
34-
expect(dbName).toEqual('static');
35-
});
36-
37-
it('should not autostart if autostart: false', async () => {
38-
replSet = new MongoMemoryReplSet({ autoStart: false });
39-
await new Promise((resolve, reject) => {
40-
replSet.once('state', (state) => reject(new Error(`Invalid state: ${state}`)));
41-
setTimeout(resolve, 500);
42-
});
43-
});
44-
45-
it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
46-
replSet = new MongoMemoryReplSet();
47-
await replSet.waitUntilRunning();
48-
const uri = await replSet.getUri();
49-
50-
await MongoClient.connect(`${uri}?replicaSet=testset`, {
51-
useNewUrlParser: true,
52-
useUnifiedTopology: true,
53-
});
54-
});
55-
});
56-
576
describe('multi-member replica set', () => {
58-
let replSet: MongoMemoryReplSet;
59-
afterEach(async () => {
60-
if (!replSet) return;
61-
await replSet.stop();
62-
});
63-
64-
it('should enter running state', async () => {
7+
it.only('should enter running state', async () => {
658
const opts: any = { replSet: { count: 3 } };
66-
replSet = new MongoMemoryReplSet(opts);
9+
const replSet = new MongoMemoryReplSet(opts);
6710
await replSet.waitUntilRunning();
6811
expect(replSet.servers.length).toEqual(3);
6912
const uri = await replSet.getUri();
7013
expect(uri.split(',').length).toEqual(3);
14+
15+
await replSet.stop();
7116
}, 40000);
7217

7318
it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
7419
const opts: any = { replSet: { count: 3 } };
75-
replSet = new MongoMemoryReplSet(opts);
20+
const replSet = new MongoMemoryReplSet(opts);
7621
await replSet.waitUntilRunning();
7722
const uri = await replSet.getUri();
7823

7924
await MongoClient.connect(`${uri}?replicaSet=testset`, {
8025
useNewUrlParser: true,
8126
useUnifiedTopology: true,
8227
});
28+
29+
await replSet.stop();
8330
});
8431
});

0 commit comments

Comments
 (0)