forked from shelfio/jest-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.js
54 lines (38 loc) · 1.38 KB
/
environment.js
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
44
45
46
47
48
49
50
51
52
53
54
const NodeEnvironment = require('jest-environment-node');
const path = require('path');
const fs = require('fs');
const uuid = require('uuid');
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
const {getMongodbMemoryOptions} = require('./helpers');
const debug = require('debug')('jest-mongodb:environment');
const cwd = process.cwd();
const globalConfigPath = path.join(cwd, 'globalConfig.json');
const options = getMongodbMemoryOptions();
const isReplSet = Boolean(options.replSet);
debug(`isReplSet`, isReplSet);
let mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
module.exports = class MongoEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
}
async setup() {
debug('Setup MongoDB Test Environment');
const globalConfig = JSON.parse(fs.readFileSync(globalConfigPath, 'utf-8'));
if (globalConfig.mongoUri) {
this.global.__MONGO_URI__ = globalConfig.mongoUri;
} else {
await mongo.start();
this.global.__MONGO_URI__ = mongo.getUri();
}
this.global.__MONGO_DB_NAME__ = globalConfig.mongoDBName || uuid.v4();
await super.setup();
}
async teardown() {
debug('Teardown MongoDB Test Environment');
await mongo.stop();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
};