From 18dcf3ca33b6136c91bc35f6c72bd5393b4818d4 Mon Sep 17 00:00:00 2001 From: Rodolfo Silva Date: Sun, 21 Oct 2018 10:01:10 -0300 Subject: [PATCH] Update MongoDB.md (#7230) --- docs/MongoDB.md | 12 +++-- .../versioned_docs/version-22.3/MongoDB.md | 52 +++++++++++++------ .../versioned_docs/version-23.0/MongoDB.md | 12 +++-- .../versioned_docs/version-23.2/MongoDB.md | 12 +++-- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/docs/MongoDB.md b/docs/MongoDB.md index 6435d2010484..6d4fcbf88108 100644 --- a/docs/MongoDB.md +++ b/docs/MongoDB.md @@ -22,13 +22,19 @@ const path = require('path'); const fs = require('fs'); -const MongodbMemoryServer = require('mongodb-memory-server'); +const {MongoMemoryServer} = require('mongodb-memory-server'); const globalConfigPath = path.join(__dirname, 'globalConfig.json'); -const mongoServer = new MongodbMemoryServer.MongoMemoryServer(); +const mongoServer = new MongoMemoryServer({ + autoStart: false, +}); + +module.exports = async () => { + if (!mongod.isRunning) { + await mongod.start(); + } -module.exports = async function() { const mongoConfig = { mongoDBName: 'jest', mongoUri: await mongoServer.getConnectionString(), diff --git a/website/versioned_docs/version-22.3/MongoDB.md b/website/versioned_docs/version-22.3/MongoDB.md index da6bb547b8e1..edbbf86e44c3 100644 --- a/website/versioned_docs/version-22.3/MongoDB.md +++ b/website/versioned_docs/version-22.3/MongoDB.md @@ -19,21 +19,33 @@ 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', - }, +const path = require('path'); + +const fs = require('fs'); + +const {MongoMemoryServer} = require('mongodb-memory-server'); + +const globalConfigPath = path.join(__dirname, 'globalConfig.json'); + +const mongoServer = new MongoMemoryServer({ + autoStart: false, }); -module.exports = function() { - global.__MONGOD__ = mongod; - global.__MONGO_DB_NAME__ = MONGO_DB_NAME; +module.exports = async () => { + if (!mongod.isRunning) { + await mongod.start(); + } + + const mongoConfig = { + mongoDBName: 'jest', + mongoUri: await mongoServer.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__ = mongoServer; }; ``` @@ -41,6 +53,14 @@ 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); @@ -49,8 +69,10 @@ class MongoEnvironment extends NodeEnvironment { 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(); } diff --git a/website/versioned_docs/version-23.0/MongoDB.md b/website/versioned_docs/version-23.0/MongoDB.md index 5709e7664285..f12a6d257b31 100644 --- a/website/versioned_docs/version-23.0/MongoDB.md +++ b/website/versioned_docs/version-23.0/MongoDB.md @@ -23,13 +23,19 @@ const path = require('path'); const fs = require('fs'); -const MongodbMemoryServer = require('mongodb-memory-server'); +const {MongoMemoryServer} = require('mongodb-memory-server'); const globalConfigPath = path.join(__dirname, 'globalConfig.json'); -const mongoServer = new MongodbMemoryServer.MongoMemoryServer(); +const mongoServer = new MongoMemoryServer({ + autoStart: false, +}); + +module.exports = async () => { + if (!mongod.isRunning) { + await mongod.start(); + } -module.exports = async function() { const mongoConfig = { mongoDBName: 'jest', mongoUri: await mongoServer.getConnectionString(), diff --git a/website/versioned_docs/version-23.2/MongoDB.md b/website/versioned_docs/version-23.2/MongoDB.md index 71d9ede3401f..e229d0fa0798 100644 --- a/website/versioned_docs/version-23.2/MongoDB.md +++ b/website/versioned_docs/version-23.2/MongoDB.md @@ -23,13 +23,19 @@ const path = require('path'); const fs = require('fs'); -const MongodbMemoryServer = require('mongodb-memory-server'); +const {MongoMemoryServer} = require('mongodb-memory-server'); const globalConfigPath = path.join(__dirname, 'globalConfig.json'); -const mongoServer = new MongodbMemoryServer.MongoMemoryServer(); +const mongoServer = new MongoMemoryServer({ + autoStart: false, +}); + +module.exports = async () => { + if (!mongod.isRunning) { + await mongod.start(); + } -module.exports = async function() { const mongoConfig = { mongoDBName: 'jest', mongoUri: await mongoServer.getConnectionString(),