Skip to content

Commit 5a35d34

Browse files
author
malo
committed
add changes to MongoDB.md to the versioned 23.0 directory
1 parent 19035af commit 5a35d34

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

website/versioned_docs/version-23.0/MongoDB.md

+27-13
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@ Here's an example of the GlobalSetup script
1919

2020
```js
2121
// setup.js
22+
const path = require('path');
23+
24+
const fs = require('fs');
25+
2226
const MongodbMemoryServer = require('mongodb-memory-server');
2327

24-
const MONGO_DB_NAME = 'jest';
25-
const mongod = new MongodbMemoryServer.default({
26-
instance: {
27-
dbName: MONGO_DB_NAME,
28-
},
29-
binary: {
30-
version: '3.2.19',
31-
},
32-
});
28+
const globalConfigPath = path.join(__dirname, 'globalConfig.json');
29+
30+
const mongoServer = new MongodbMemoryServer.MongoMemoryServer();
31+
32+
module.exports = async function() {
33+
const mongoConfig = {
34+
mongoDBName: 'jest',
35+
mongoUri: await mongoServer.getConnectionString(),
36+
};
3337

34-
module.exports = function() {
38+
// Write global config to disk because all tests run in different contexts.
39+
fs.writeFileSync(mongoFileConfigPath, JSON.stringify(mongoConfig));
40+
41+
// Set reference to mongod in order to close the server during teardown.
3542
global.__MONGOD__ = mongod;
36-
global.__MONGO_DB_NAME__ = MONGO_DB_NAME;
3743
};
3844
```
3945

@@ -43,6 +49,12 @@ Then we need a custom Test Environment for Mongo
4349
// mongo-environment.js
4450
const NodeEnvironment = require('jest-environment-node');
4551

52+
const path = require('path');
53+
54+
const fs = require('fs');
55+
56+
const globalConfigPath = path.join(__dirname, 'globalConfig.json');
57+
4658
class MongoEnvironment extends NodeEnvironment {
4759
constructor(config) {
4860
super(config);
@@ -51,8 +63,10 @@ class MongoEnvironment extends NodeEnvironment {
5163
async setup() {
5264
console.log('Setup MongoDB Test Environment');
5365

54-
this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString();
55-
this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__;
66+
const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8'));
67+
68+
this.global.__MONGO_URI__ = globalConfig.mongoUri;
69+
this.global.__MONGO_DB_NAME__ = globalConfig.mongoDBName;
5670

5771
await super.setup();
5872
}

0 commit comments

Comments
 (0)