Skip to content

Commit

Permalink
feat: update to latest jest & fix related problems
Browse files Browse the repository at this point in the history
  • Loading branch information
vladholubiev committed Jul 21, 2018
1 parent 3763fa8 commit 2e7133a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ temp
yarn.lock
*.log
.DS_Store
globalConfig.json
58 changes: 30 additions & 28 deletions mongo-aggregate.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
const {MongoClient} = require('mongodb');

let connection;
let db;
describe('insert', () => {
let connection;
let db;

beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__);
db = await connection.db(global.__MONGO_DB_NAME__);
});
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__);
db = await connection.db(global.__MONGO_DB_NAME__);
});

afterAll(async () => {
await connection.close();
await db.close();
});
afterAll(async () => {
await connection.close();
await db.close();
});

it('should aggregate docs from collection', async () => {
const files = db.collection('files');
it('should aggregate docs from collection', async () => {
const files = db.collection('files');

await files.insertMany([
{type: 'Document'},
{type: 'Video'},
{type: 'Image'},
{type: 'Document'},
{type: 'Image'},
{type: 'Document'}
]);
await files.insertMany([
{type: 'Document'},
{type: 'Video'},
{type: 'Image'},
{type: 'Document'},
{type: 'Image'},
{type: 'Document'}
]);

const topFiles = await files
.aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}])
.toArray();
const topFiles = await files
.aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}])
.toArray();

expect(topFiles).toEqual([
{_id: 'Document', count: 3},
{_id: 'Image', count: 2},
{_id: 'Video', count: 1}
]);
expect(topFiles).toEqual([
{_id: 'Document', count: 3},
{_id: 'Image', count: 2},
{_id: 'Video', count: 1}
]);
});
});
15 changes: 9 additions & 6 deletions mongo-environment.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const NodeEnvironment = require('jest-environment-node');
const path = require('path');
const fs = require('fs');
const globalConfigPath = path.join(__dirname, 'globalConfig.json');

class MongoEnvironment extends NodeEnvironment {
module.exports = class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}

async setup() {
console.log('Setup MongoDB Test Environment');

this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8'));

this.global.__MONGO_URI__ = globalConfig.mongoUri;
this.global.__MONGO_DB_NAME__ = globalConfig.mongoDBName;

await super.setup();
}
Expand All @@ -23,6 +28,4 @@ class MongoEnvironment extends NodeEnvironment {
runScript(script) {
return super.runScript(script);
}
}

module.exports = MongoEnvironment;
};
72 changes: 37 additions & 35 deletions mongo-insert.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
const {MongoClient} = require('mongodb');

let connection;
let db;

beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__);
db = await connection.db(global.__MONGO_DB_NAME__);
});

afterAll(async () => {
await connection.close();
await db.close();
});

it('should insert a doc into collection', async () => {
const users = db.collection('users');

const mockUser = {_id: 'some-user-id', name: 'John'};
await users.insertOne(mockUser);

const insertedUser = await users.findOne({_id: 'some-user-id'});
expect(insertedUser).toEqual(mockUser);
});

it('should insert many docs into collection', async () => {
const users = db.collection('users');

const mockUsers = [{name: 'Alice'}, {name: 'Bob'}];
await users.insertMany(mockUsers);

const insertedUsers = await users.find().toArray();
expect(insertedUsers).toEqual([
expect.objectContaining({name: 'John'}),
expect.objectContaining({name: 'Alice'}),
expect.objectContaining({name: 'Bob'})
]);
describe('insert', () => {
let connection;
let db;

beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__);
db = await connection.db(global.__MONGO_DB_NAME__);
});

afterAll(async () => {
await connection.close();
await db.close();
});

it('should insert a doc into collection', async () => {
const users = db.collection('users');

const mockUser = {_id: 'some-user-id', name: 'John'};
await users.insertOne(mockUser);

const insertedUser = await users.findOne({_id: 'some-user-id'});
expect(insertedUser).toEqual(mockUser);
});

it('should insert many docs into collection', async () => {
const users = db.collection('users');

const mockUsers = [{name: 'Alice'}, {name: 'Bob'}];
await users.insertMany(mockUsers);

const insertedUsers = await users.find().toArray();
expect(insertedUsers).toEqual([
expect.objectContaining({name: 'John'}),
expect.objectContaining({name: 'Alice'}),
expect.objectContaining({name: 'Bob'})
]);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"private": true,
"scripts": {
"lint": "eslint . --fix",
"test": "jest --runInBand"
"test": "jest"
},
"keywords": [],
"dependencies": {
Expand Down
22 changes: 17 additions & 5 deletions setup.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
const path = require('path');
const fs = require('fs');
const MongodbMemoryServer = require('mongodb-memory-server');
const globalConfigPath = path.join(__dirname, 'globalConfig.json');

const MONGO_DB_NAME = 'jest';
const mongod = new MongodbMemoryServer.default({
instance: {
dbName: MONGO_DB_NAME
dbName: 'jest'
},
binary: {
version: '3.2.19'
version: '3.2.18'
}
});

module.exports = function() {
module.exports = async function() {
const mongoConfig = {
mongoDBName: 'jest',
mongoUri: await mongod.getConnectionString()
};

// Write global config to disk because all tests run in different contexts.
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
console.log('Config is written');

// Set reference to mongod in order to close the server during teardown.
global.__MONGOD__ = mongod;
global.__MONGO_DB_NAME__ = MONGO_DB_NAME;
process.env.MONGO_URL = mongoConfig.mongoUri;
};

0 comments on commit 2e7133a

Please sign in to comment.