1 | const fs = require('fs');
|
2 | const {join} = require('path');
|
3 | const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
|
4 | const {
|
5 | getMongodbMemoryOptions,
|
6 | getMongoURLEnvName,
|
7 | shouldUseSharedDBForAllJestWorkers,
|
8 | } = require('./helpers');
|
9 |
|
10 | const debug = require('debug')('jest-mongodb:setup');
|
11 | const mongoMemoryServerOptions = getMongodbMemoryOptions();
|
12 | const isReplSet = Boolean(mongoMemoryServerOptions.replSet);
|
13 |
|
14 | debug(`isReplSet ${isReplSet}`);
|
15 |
|
16 | const mongo = isReplSet
|
17 | ? new MongoMemoryReplSet(mongoMemoryServerOptions)
|
18 | : new MongoMemoryServer(mongoMemoryServerOptions);
|
19 |
|
20 | const cwd = process.cwd();
|
21 | const globalConfigPath = join(cwd, 'globalConfig.json');
|
22 |
|
23 | module.exports = async () => {
|
24 | const options = getMongodbMemoryOptions();
|
25 | const mongoConfig = {};
|
26 |
|
27 | debug(`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers()}`);
|
28 |
|
29 |
|
30 | if (shouldUseSharedDBForAllJestWorkers()) {
|
31 | if (!mongo.isRunning) {
|
32 | await mongo.start();
|
33 | }
|
34 |
|
35 | const mongoURLEnvName = getMongoURLEnvName();
|
36 |
|
37 | mongoConfig.mongoUri = await mongo.getUri();
|
38 |
|
39 | process.env[mongoURLEnvName] = mongoConfig.mongoUri;
|
40 |
|
41 |
|
42 | global.__MONGOD__ = mongo;
|
43 | }
|
44 |
|
45 | mongoConfig.mongoDBName = options.instance.dbName;
|
46 |
|
47 |
|
48 | fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
|
49 | debug('Config is written');
|
50 | };
|