From 8016ccab59f05b8997624f07f348544587e9fdb0 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:03:31 +0300 Subject: [PATCH 1/6] docs: simplify mongodb example by using preset I've just published a Jest preset to simplify usage. Since I was the author of the original tutorial, the code underneath remained the same --- docs/MongoDB.md | 153 +++++++++++------------------------------------- 1 file changed, 33 insertions(+), 120 deletions(-) diff --git a/docs/MongoDB.md b/docs/MongoDB.md index f12bdca10018..0705a937baed 100644 --- a/docs/MongoDB.md +++ b/docs/MongoDB.md @@ -5,142 +5,55 @@ 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 +## Use jest-mongodb Preset -The basic idea is to: +[Jest MongoDB](https://github.com/shelfio/jest-mongodb) provides all required configuration to run your tests using MongoDB. -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 +1. First install `@shelf/jest-mongodb` -Here's an example of the GlobalSetup script - -```js -// setup.js -const path = require('path'); - -const fs = require('fs'); - -const {MongoMemoryServer} = require('mongodb-memory-server'); - -const globalConfigPath = path.join(__dirname, 'globalConfig.json'); - -const mongod = new MongoMemoryServer({ - autoStart: false, -}); - -module.exports = async () => { - if (!mongod.isRunning) { - await mongod.start(); - } - - 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)); - - // Set reference to mongod in order to close the server during teardown. - global.__MONGOD__ = mongod; -}; +``` +yarn add @shelf/jest-mongodb --dev ``` -Then we need a custom Test Environment for Mongo - -```js -// mongo-environment.js -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 { - constructor(config) { - super(config); - } - - async setup() { - console.log('Setup MongoDB Test Environment'); - - 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(); - } - - async teardown() { - console.log('Teardown MongoDB Test Environment'); - - await super.teardown(); - } +2. Specify preset in your Jest configuration: - runScript(script) { - return super.runScript(script); - } +```json +{ + "preset": "@shelf/jest-mongodb" } - -module.exports = MongoEnvironment; ``` -Finally we can shut down mongodb server +3. Write your test ```js -// teardown.js -module.exports = async function() { - await global.__MONGOD__.stop(); -}; -``` +const {MongoClient} = require('mongodb'); -With all the things set up, we can now write our tests like this: +describe('insert', () => { + let connection; + let db; -```js -// test.js -const {MongoClient} = require('mongodb'); + beforeAll(async () => { + connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true}); + db = await connection.db(global.__MONGO_DB_NAME__); + }); -let connection; -let db; + afterAll(async () => { + await connection.close(); + await db.close(); + }); -beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__); - db = await connection.db(global.__MONGO_DB_NAME__); -}); + it('should insert a doc into collection', async () => { + const users = db.collection('users'); -afterAll(async () => { - await connection.close(); - await db.close(); -}); + const mockUser = {_id: 'some-user-id', name: 'John'}; + await users.insertOne(mockUser); -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}, - ]); + const insertedUser = await users.findOne({_id: 'some-user-id'}); + expect(insertedUser).toEqual(mockUser); + }); }); ``` + +There's no need to load any dependencies. + +See [documentation](https://github.com/shelfio/jest-mongodb) for details (configuring MongoDB version, etc). From 44191a1e5cc528339daf1f9ec51a3b8b97faf907 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:06:30 +0300 Subject: [PATCH 2/6] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e23216d3f0a3..0d7f5384a512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Chore & Maintenance - `[expect]` Fix label and add opposite assertion for toEqual tests ([#8288](https://github.com/facebook/jest/pull/8288)) +- `[docs]` Mention Jest MongoDB Preset ([#8318](https://github.com/facebook/jest/pull/8318)) ### Performance From 0d1440aea53b381353bf969abb8b8dc57ffc2b6e Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Fri, 12 Apr 2019 21:25:01 +0300 Subject: [PATCH 3/6] style: apply prettier style suggestions --- docs/MongoDB.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/MongoDB.md b/docs/MongoDB.md index 0705a937baed..3610da622673 100644 --- a/docs/MongoDB.md +++ b/docs/MongoDB.md @@ -33,7 +33,9 @@ describe('insert', () => { let db; beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true}); + connection = await MongoClient.connect(global.__MONGO_URI__, { + useNewUrlParser: true, + }); db = await connection.db(global.__MONGO_DB_NAME__); }); @@ -54,6 +56,6 @@ describe('insert', () => { }); ``` -There's no need to load any dependencies. +There's no need to load any dependencies. See [documentation](https://github.com/shelfio/jest-mongodb) for details (configuring MongoDB version, etc). From ca656e040b071e842503f6f18249b83cf8c787d3 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Thu, 18 Apr 2019 14:00:02 +0300 Subject: [PATCH 4/6] docs: update v23 version doc --- .../versioned_docs/version-23.x/MongoDB.md | 156 ++++-------------- 1 file changed, 36 insertions(+), 120 deletions(-) diff --git a/website/versioned_docs/version-23.x/MongoDB.md b/website/versioned_docs/version-23.x/MongoDB.md index 9606b0f9eb3f..3610da622673 100644 --- a/website/versioned_docs/version-23.x/MongoDB.md +++ b/website/versioned_docs/version-23.x/MongoDB.md @@ -1,145 +1,61 @@ --- -id: version-23.x-mongodb +id: mongodb title: Using with MongoDB -original_id: 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 +## Use jest-mongodb Preset -The basic idea is to: +[Jest MongoDB](https://github.com/shelfio/jest-mongodb) provides all required configuration to run your tests using MongoDB. -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 +1. First install `@shelf/jest-mongodb` -Here's an example of the GlobalSetup script - -```js -// setup.js -const path = require('path'); - -const fs = require('fs'); - -const {MongoMemoryServer} = require('mongodb-memory-server'); - -const globalConfigPath = path.join(__dirname, 'globalConfig.json'); - -const mongod = new MongoMemoryServer({ - autoStart: false, -}); - -module.exports = async () => { - if (!mongod.isRunning) { - await mongod.start(); - } - - 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)); - - // Set reference to mongod in order to close the server during teardown. - global.__MONGOD__ = mongod; -}; +``` +yarn add @shelf/jest-mongodb --dev ``` -Then we need a custom Test Environment for Mongo - -```js -// mongo-environment.js -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 { - constructor(config) { - super(config); - } - - async setup() { - console.log('Setup MongoDB Test Environment'); - - 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(); - } - - async teardown() { - console.log('Teardown MongoDB Test Environment'); - - await super.teardown(); - } +2. Specify preset in your Jest configuration: - runScript(script) { - return super.runScript(script); - } +```json +{ + "preset": "@shelf/jest-mongodb" } ``` -Finally we can shut down mongodb server +3. Write your test ```js -// teardown.js -module.exports = async function() { - await global.__MONGOD__.stop(); -}; -``` +const {MongoClient} = require('mongodb'); -With all the things set up, we can now write our tests like this: +describe('insert', () => { + let connection; + let db; -```js -// test.js -const {MongoClient} = require('mongodb'); + beforeAll(async () => { + connection = await MongoClient.connect(global.__MONGO_URI__, { + useNewUrlParser: true, + }); + db = await connection.db(global.__MONGO_DB_NAME__); + }); -let connection; -let db; + afterAll(async () => { + await connection.close(); + await db.close(); + }); -beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__); - db = await connection.db(global.__MONGO_DB_NAME__); -}); + it('should insert a doc into collection', async () => { + const users = db.collection('users'); -afterAll(async () => { - await connection.close(); - await db.close(); -}); + const mockUser = {_id: 'some-user-id', name: 'John'}; + await users.insertOne(mockUser); -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}, - ]); + const insertedUser = await users.findOne({_id: 'some-user-id'}); + expect(insertedUser).toEqual(mockUser); + }); }); ``` + +There's no need to load any dependencies. + +See [documentation](https://github.com/shelfio/jest-mongodb) for details (configuring MongoDB version, etc). From 6d1d00cea9e30c2b19cf534c6c5d33e9316692a5 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Thu, 18 Apr 2019 14:01:25 +0300 Subject: [PATCH 5/6] docs: leave original page metadata for v23 --- website/versioned_docs/version-23.x/MongoDB.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website/versioned_docs/version-23.x/MongoDB.md b/website/versioned_docs/version-23.x/MongoDB.md index 3610da622673..df779d2b6b2b 100644 --- a/website/versioned_docs/version-23.x/MongoDB.md +++ b/website/versioned_docs/version-23.x/MongoDB.md @@ -1,6 +1,7 @@ --- -id: mongodb +id: version-23.x-mongodb title: Using with MongoDB +original_id: 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/). From b13c7354f41843c3b8885b4c9c82e2d8065957e9 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Thu, 18 Apr 2019 14:01:50 +0300 Subject: [PATCH 6/6] docs: update v24 versioned doc page --- .../versioned_docs/version-24.0/MongoDB.md | 155 ++++-------------- 1 file changed, 35 insertions(+), 120 deletions(-) diff --git a/website/versioned_docs/version-24.0/MongoDB.md b/website/versioned_docs/version-24.0/MongoDB.md index dc1f02ff4c14..23ba1283162e 100644 --- a/website/versioned_docs/version-24.0/MongoDB.md +++ b/website/versioned_docs/version-24.0/MongoDB.md @@ -6,142 +6,57 @@ original_id: 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 +## Use jest-mongodb Preset -The basic idea is to: +[Jest MongoDB](https://github.com/shelfio/jest-mongodb) provides all required configuration to run your tests using MongoDB. -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 +1. First install `@shelf/jest-mongodb` -Here's an example of the GlobalSetup script - -```js -// setup.js -const path = require('path'); - -const fs = require('fs'); - -const {MongoMemoryServer} = require('mongodb-memory-server'); - -const globalConfigPath = path.join(__dirname, 'globalConfig.json'); - -const mongod = new MongoMemoryServer({ - autoStart: false, -}); - -module.exports = async () => { - if (!mongod.isRunning) { - await mongod.start(); - } - - 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)); - - // Set reference to mongod in order to close the server during teardown. - global.__MONGOD__ = mongod; -}; +``` +yarn add @shelf/jest-mongodb --dev ``` -Then we need a custom Test Environment for Mongo - -```js -// mongo-environment.js -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 { - constructor(config) { - super(config); - } - - async setup() { - console.log('Setup MongoDB Test Environment'); - - 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(); - } - - async teardown() { - console.log('Teardown MongoDB Test Environment'); - - await super.teardown(); - } +2. Specify preset in your Jest configuration: - runScript(script) { - return super.runScript(script); - } +```json +{ + "preset": "@shelf/jest-mongodb" } - -module.exports = MongoEnvironment; ``` -Finally we can shut down mongodb server +3. Write your test ```js -// teardown.js -module.exports = async function() { - await global.__MONGOD__.stop(); -}; -``` +const {MongoClient} = require('mongodb'); -With all the things set up, we can now write our tests like this: +describe('insert', () => { + let connection; + let db; -```js -// test.js -const {MongoClient} = require('mongodb'); + beforeAll(async () => { + connection = await MongoClient.connect(global.__MONGO_URI__, { + useNewUrlParser: true, + }); + db = await connection.db(global.__MONGO_DB_NAME__); + }); -let connection; -let db; + afterAll(async () => { + await connection.close(); + await db.close(); + }); -beforeAll(async () => { - connection = await MongoClient.connect(global.__MONGO_URI__); - db = await connection.db(global.__MONGO_DB_NAME__); -}); + it('should insert a doc into collection', async () => { + const users = db.collection('users'); -afterAll(async () => { - await connection.close(); - await db.close(); -}); + const mockUser = {_id: 'some-user-id', name: 'John'}; + await users.insertOne(mockUser); -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}, - ]); + const insertedUser = await users.findOne({_id: 'some-user-id'}); + expect(insertedUser).toEqual(mockUser); + }); }); ``` + +There's no need to load any dependencies. + +See [documentation](https://github.com/shelfio/jest-mongodb) for details (configuring MongoDB version, etc).