Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add jest-mongodb example #5571

Merged
merged 5 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions docs/MongoDB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
id: mongodb
title: Using with MongoDB
---

With the [Global Setup/Teardown](Configuration.md#globalsetup-string) and
[Async Test Environment](Configuration.md#testenvironment-string) APIs, Jest can
work smoothly with [MongoDB](https://www.mongodb.com/).

## A jest-mongodb example

The basic idea is to:

1. Spin up in-memory mongodb server
2. Export a global variable with mongo URI
3. Write tests for queries / aggregations using a real database ✨
4. Shut down mongodb server using Global Teardown

Here's an example of the GlobalSetup script

```js
// setup.js
const MongodbMemoryServer = require('mongodb-memory-server');

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

module.exports = function() {
global.__MONGOD__ = mongod;
global.__MONGO_DB_NAME__ = MONGO_DB_NAME;
};
```

Then we need a custom Test Environment for Mongo

```js
// mongo-environment.js
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__;

await super.setup();
}

async teardown() {
console.log('Teardown MongoDB Test Environment');

await super.teardown();
}

runScript(script) {
return super.runScript(script);
}
}
```

Finally we can shut down mongodb server

```js
// teardown.js
module.exports = async function() {
await global.__MONGOD__.stop();
};
```

With all the things set up, we can now write our tests like this:

```js
// test.js
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 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'}
]);

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}
]);
});
```

Here's the code of
[full working example](https://github.com/vladgolubev/jest-mongodb).
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"es6-class-mocks",
"webpack",
"puppeteer",
"mongodb",
"migration-guide",
"troubleshooting"
],
Expand Down