test: refactor ReplSet tests, split them for better debugging

This commit is contained in:
nodkz
2019-10-22 21:26:08 +06:00
parent d961c45785
commit 1be8255746
2 changed files with 66 additions and 60 deletions
@@ -0,0 +1,59 @@
import MongoMemoryReplSet from '../MongoMemoryReplSet';
import { MongoClient } from 'mongodb';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
describe('single server replset', () => {
it('should enter running state', async () => {
const replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
expect(uri.split(',').length).toEqual(1);
await replSet.stop();
});
it('should be able to get connection string to specific db', async () => {
const replSet = new MongoMemoryReplSet({});
await replSet.waitUntilRunning();
const uri = await replSet.getUri('other');
const str = await replSet.getConnectionString('other');
expect(uri.split(',').length).toEqual(1);
expect(uri.endsWith('/other')).toBeTruthy();
expect(str).toEqual(uri);
await replSet.stop();
});
it('should be able to get dbName', async () => {
const opts: any = { autoStart: false, replSet: { dbName: 'static' } };
const replSet = new MongoMemoryReplSet(opts);
const dbName = await replSet.getDbName();
expect(dbName).toEqual('static');
await replSet.stop();
});
it('should not autostart if autostart: false', async () => {
const replSet = new MongoMemoryReplSet({ autoStart: false });
await new Promise((resolve, reject) => {
replSet.once('state', (state) => reject(new Error(`Invalid state: ${state}`)));
setTimeout(resolve, 500);
});
await replSet.stop();
});
it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
const replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
await MongoClient.connect(`${uri}?replicaSet=testset`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await replSet.stop();
});
});
@@ -3,76 +3,21 @@ import { MongoClient } from 'mongodb';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
describe('single server replset', () => {
let replSet: MongoMemoryReplSet;
afterEach(async () => {
if (!(replSet && replSet._state === 'running')) return;
await replSet.stop();
});
it('should enter running state', async () => {
replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
expect(uri.split(',').length).toEqual(1);
});
it('should be able to get connection string to specific db', async () => {
replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const uri = await replSet.getUri('other');
const str = await replSet.getConnectionString('other');
expect(uri.split(',').length).toEqual(1);
expect(uri.endsWith('/other')).toBeTruthy();
expect(str).toEqual(uri);
});
it('should be able to get dbName', async () => {
const opts: any = { autoStart: false, replSet: { dbName: 'static' } };
replSet = new MongoMemoryReplSet(opts);
const dbName = await replSet.getDbName();
expect(dbName).toEqual('static');
});
it('should not autostart if autostart: false', async () => {
replSet = new MongoMemoryReplSet({ autoStart: false });
await new Promise((resolve, reject) => {
replSet.once('state', (state) => reject(new Error(`Invalid state: ${state}`)));
setTimeout(resolve, 500);
});
});
it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
replSet = new MongoMemoryReplSet();
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
await MongoClient.connect(`${uri}?replicaSet=testset`, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
});
});
describe('multi-member replica set', () => {
let replSet: MongoMemoryReplSet;
afterEach(async () => {
if (!replSet) return;
await replSet.stop();
});
it('should enter running state', async () => {
it.only('should enter running state', async () => {
const opts: any = { replSet: { count: 3 } };
replSet = new MongoMemoryReplSet(opts);
const replSet = new MongoMemoryReplSet(opts);
await replSet.waitUntilRunning();
expect(replSet.servers.length).toEqual(3);
const uri = await replSet.getUri();
expect(uri.split(',').length).toEqual(3);
await replSet.stop();
}, 40000);
it('should be possible to connect replicaset after waitUntilRunning resolveds', async () => {
const opts: any = { replSet: { count: 3 } };
replSet = new MongoMemoryReplSet(opts);
const replSet = new MongoMemoryReplSet(opts);
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
@@ -80,5 +25,7 @@ describe('multi-member replica set', () => {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await replSet.stop();
});
});