Skip to content

Commit

Permalink
Update MongoDB.md (#7230)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodolfoSilva authored and SimenB committed Oct 21, 2018
1 parent 7be2a8e commit 18dcf3c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
12 changes: 9 additions & 3 deletions docs/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
52 changes: 37 additions & 15 deletions website/versioned_docs/version-22.3/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,48 @@ 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;
};
```

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);
Expand All @@ -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();
}
Expand Down
12 changes: 9 additions & 3 deletions website/versioned_docs/version-23.0/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 9 additions & 3 deletions website/versioned_docs/version-23.2/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 18dcf3c

Please sign in to comment.