forked from shelfio/jest-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
43 lines (32 loc) · 1.27 KB
/
Copy pathsetup.js
File metadata and controls
43 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const fs = require('fs');
const {join} = require('path');
const {MongoMemoryServer} = require('mongodb-memory-server');
const {
getMongodbMemoryOptions,
getMongoURLEnvName,
shouldUseSharedDBForAllJestWorkers,
} = require('./helpers');
const debug = require('debug')('jest-mongodb:setup');
const mongod = new MongoMemoryServer(getMongodbMemoryOptions());
const cwd = process.cwd();
const globalConfigPath = join(cwd, 'globalConfig.json');
module.exports = async () => {
const options = getMongodbMemoryOptions();
const mongoConfig = {};
debug(`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers()}`);
// if we run one mongodb instance for all tests
if (shouldUseSharedDBForAllJestWorkers()) {
if (!mongod.isRunning) {
await mongod.start();
}
const mongoURLEnvName = getMongoURLEnvName();
mongoConfig.mongoUri = await mongod.getUri();
process.env[mongoURLEnvName] = mongoConfig.mongoUri;
// Set reference to mongod in order to close the server during teardown.
global.__MONGOD__ = mongod;
}
mongoConfig.mongoDBName = options.instance.dbName;
// Write global config to disk because all tests run in different contexts.
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
debug('Config is written');
};