1 | const fs = require('fs');
|
2 | const {resolve, join} = require('path');
|
3 | const cwd = require('cwd');
|
4 | const MongodbMemoryServer = require('mongodb-memory-server');
|
5 | const globalConfigPath = join(__dirname, 'globalConfig.json');
|
6 |
|
7 | const mongod = new MongodbMemoryServer.default(getMongodbMemoryOptions());
|
8 |
|
9 | module.exports = async () => {
|
10 | if (!mongod.isRunning) {
|
11 | await mongod.start();
|
12 | }
|
13 |
|
14 | const mongoConfig = {
|
15 | mongoDBName: 'jest',
|
16 | mongoUri: await mongod.getConnectionString()
|
17 | };
|
18 |
|
19 |
|
20 | fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig));
|
21 | console.log('Config is written');
|
22 |
|
23 |
|
24 | global.__MONGOD__ = mongod;
|
25 | process.env.MONGO_URL = mongoConfig.mongoUri;
|
26 | };
|
27 |
|
28 | function getMongodbMemoryOptions() {
|
29 | try {
|
30 | const {mongodbMemoryServerOptions} = require(resolve(cwd(), 'jest-mongodb-config.js'));
|
31 |
|
32 | return mongodbMemoryServerOptions;
|
33 | } catch (e) {
|
34 | return {
|
35 | instance: {
|
36 | dbName: 'jest'
|
37 | },
|
38 | binary: {
|
39 | skipMD5: true
|
40 | },
|
41 | autoStart: false
|
42 | };
|
43 | }
|
44 | }
|