-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-environment-mongodb.js
45 lines (35 loc) · 986 Bytes
/
jest-environment-mongodb.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
// eslint-disable-next-line
const MongodbMemoryServer = require('mongodb-memory-server').default;
// eslint-disable-next-line
const NodeEnvironment = require('jest-environment-node');
const mongodbMemoryServerOptions = {
instance: {},
binary: {
version: '4.2.9',
},
autoStart: false,
};
class MongodbEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
this.mongod = new MongodbMemoryServer(mongodbMemoryServerOptions);
}
async setup() {
await super.setup();
if (!this.mongod.isRunning) {
await this.mongod.start();
}
// eslint-disable-next-line
this.global.__MONGO_URI__ = await this.mongod.getConnectionString();
// eslint-disable-next-line
this.global.__MONGO_DB_NAME__ = await this.mongod.getDbName();
}
async teardown() {
await super.teardown();
await this.mongod.stop();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = MongodbEnvironment;