From b61efcb8edc5d482dc1f0f2b9093f57abe9ebfb4 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Thu, 15 Feb 2018 02:38:18 +0200 Subject: [PATCH 1/4] docs: add jest-mongodb example --- docs/MongoDB.md | 126 ++++++++++++++++++++++++++++++++++++++++++ website/sidebars.json | 1 + 2 files changed, 127 insertions(+) create mode 100644 docs/MongoDB.md diff --git a/docs/MongoDB.md b/docs/MongoDB.md new file mode 100644 index 000000000000..777db4fa8766 --- /dev/null +++ b/docs/MongoDB.md @@ -0,0 +1,126 @@ +--- +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 puppeteer + +```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 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} + ]); +}); +``` + +Note: you may need to modify test script to run jest sequentially, like that: +`jest --runInBand --forceExit`. + +Here's the code of +[full working example](https://github.com/vladgolubev/jest-mongodb). diff --git a/website/sidebars.json b/website/sidebars.json index 06978912a02e..2f5f48529200 100644 --- a/website/sidebars.json +++ b/website/sidebars.json @@ -17,6 +17,7 @@ "es6-class-mocks", "webpack", "puppeteer", + "mongodb", "migration-guide", "troubleshooting" ], From 513a6fbad495d6c72de65ede00c5c787718a4724 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Thu, 15 Feb 2018 12:03:21 +0200 Subject: [PATCH 2/4] docs: add connection.close() and remove escape hatch --forceExit --- docs/MongoDB.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/MongoDB.md b/docs/MongoDB.md index 777db4fa8766..7596bce04960 100644 --- a/docs/MongoDB.md +++ b/docs/MongoDB.md @@ -92,6 +92,7 @@ beforeAll(async () => { }); afterAll(async () => { + await connection.close(); await db.close(); }); @@ -119,8 +120,5 @@ it('should aggregate docs from collection', async () => { }); ``` -Note: you may need to modify test script to run jest sequentially, like that: -`jest --runInBand --forceExit`. - Here's the code of [full working example](https://github.com/vladgolubev/jest-mongodb). From b01beb31cccf2aa88504a4c56b2af5f7af1d6cf6 Mon Sep 17 00:00:00 2001 From: rickhanlonii Date: Thu, 15 Feb 2018 09:08:32 -0500 Subject: [PATCH 3/4] s/puppeteer/mongo --- docs/MongoDB.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/MongoDB.md b/docs/MongoDB.md index 7596bce04960..bba6a43b4039 100644 --- a/docs/MongoDB.md +++ b/docs/MongoDB.md @@ -38,7 +38,7 @@ module.exports = function() { }; ``` -Then we need a custom Test Environment for puppeteer +Then we need a custom Test Environment for Mongo ```js // mongo-environment.js From 284098d7cfba2c33a01f6171294ab2e621d8100b Mon Sep 17 00:00:00 2001 From: rickhanlonii Date: Thu, 15 Feb 2018 09:29:33 -0500 Subject: [PATCH 4/4] Add MongoDB guide to changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f9aa036b0f0..a2200bd6e435 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## master +### Features +* `[docs]` Add MongoDB guide ([#5571](https://github.com/facebook/jest/pull/5571)) + ## jest 22.3.0 ### Fixes