Skip to content

Commit

Permalink
test(Disconnect): Add closing db connection and shut downing mongod.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Jun 6, 2017
1 parent f475fbd commit 36874e2
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 76 deletions.
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,28 @@ const User = mongoose.model('User', new mongoose.Schema({ name: String })); // d
import mongoose from 'mongoose';
import MongodbMemoryServer from 'mongodb-memory-server';

let mongoServer;

before((done) => {
const mongoServer = new MongodbMemoryServer();
mongoServer = new MongodbMemoryServer();
mongoServer.getConnectionString().then((mongoUri) => {
mongoose.connect(mongoUri, (err) => {
done(err);
});
});
});

after(() => {
mongoose.disconnect();
mongoServer.stop();
});

describe('...', () => {
it("...", async () => {
const User = mongoose.model('User', new mongoose.Schema({ name: String }));
const cnt = await User.count();
expect(cnt).to.equal(0);
});
it("...", async () => {
const User = mongoose.model('User', new mongoose.Schema({ name: String }));
const cnt = await User.count();
expect(cnt).to.equal(0);
});
});
```

Expand All @@ -195,25 +202,32 @@ import MongodbMemoryServer from 'mongodb-memory-server';
// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;

let mongoServer;

beforeAll(async () => {
const mongoServer = new MongodbMemoryServer();
mongoServer = new MongodbMemoryServer();
const mongoUri = await mongoServer.getConnectionString();
mongoose.connect(mongoUri, (err) => {
console.error(err);
});
});

afterAll(() => {
mongoose.disconnect();
mongoServer.stop();
});

describe('...', () => {
it("...", async () => {
const User = mongoose.model('User', new mongoose.Schema({ name: String }));
const cnt = await User.count();
expect(cnt).toEqual(0);
});
it("...", async () => {
const User = mongoose.model('User', new mongoose.Schema({ name: String }));
const cnt = await User.count();
expect(cnt).toEqual(0);
});
});
```

Additional examples of Jest tests:
- simple example with `mongodb` in [tests in current package](https://github.com/nodkz/mongodb-memory-server/blob/master/src/__tests__/index-test.js)
- simple example with `mongodb` in [tests in current package](https://github.com/nodkz/mongodb-memory-server/blob/master/src/__tests__/)
- more complex example with `mongoose` in [graphql-compose-mongoose](https://github.com/nodkz/graphql-compose-mongoose/blob/master/src/__mocks__/mongooseCommon.js)


Expand Down
22 changes: 0 additions & 22 deletions src/__tests__/__snapshots__/index-test.js.snap

This file was deleted.

15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/multipleDB-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Multiple mongoServers should start several servers 1`] = `
Object {
"n": 2,
"ok": 1,
}
`;

exports[`Multiple mongoServers should start several servers 2`] = `
Object {
"n": 2,
"ok": 1,
}
`;
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/singleDB-test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Single mongoServer should start mongo server 1`] = `
Object {
"n": 2,
"ok": 1,
}
`;
41 changes: 0 additions & 41 deletions src/__tests__/index-test.js

This file was deleted.

48 changes: 48 additions & 0 deletions src/__tests__/multipleDB-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* @flow */

import MongoDBMemoryServer from '../index';
import { MongoClient } from 'mongodb';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;

let db1;
let db2;
let mongoServer1;
let mongoServer2;
beforeAll(async () => {
mongoServer1 = new MongoDBMemoryServer();
const mongoUri = await mongoServer1.getConnectionString();
db1 = await MongoClient.connect(mongoUri);

mongoServer2 = new MongoDBMemoryServer();
const mongoUri2 = await mongoServer2.getConnectionString();
db2 = await MongoClient.connect(mongoUri2);
});

afterAll(() => {
db1.close();
db2.close();
mongoServer1.stop();
mongoServer2.stop();
});

describe('Multiple mongoServers', () => {
it('should start several servers', async () => {
const col1 = db1.collection('test');
const result1 = await col1.insert([{ a: 1 }, { b: 1 }]);
expect(result1.result).toMatchSnapshot();
expect(await col1.count({})).toBe(2);

const col2 = db2.collection('test');
const result2 = await col2.insert([{ a: 2 }, { b: 2 }]);
expect(result2.result).toMatchSnapshot();
expect(await col2.count({})).toBe(2);
});

it('should have different uri', async () => {
const uri1 = await mongoServer1.getConnectionString();
const uri2 = await mongoServer2.getConnectionString();

expect(uri1).not.toEqual(uri2);
});
});
28 changes: 28 additions & 0 deletions src/__tests__/singleDB-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @flow */

import MongoDBMemoryServer from '../index';
import { MongoClient } from 'mongodb';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;

let db;
let mongoServer;
beforeAll(async () => {
mongoServer = new MongoDBMemoryServer();
const mongoUri = await mongoServer.getConnectionString();
db = await MongoClient.connect(mongoUri);
});

afterAll(() => {
db.close();
mongoServer.stop();
});

describe('Single mongoServer', () => {
it('should start mongo server', async () => {
const col = db.collection('test');
const result = await col.insert([{ a: 1 }, { b: 1 }]);
expect(result.result).toMatchSnapshot();
expect(await col.count({})).toBe(2);
});
});

0 comments on commit 36874e2

Please sign in to comment.