-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
mocha-fixtures.js
56 lines (44 loc) · 2.03 KB
/
mocha-fixtures.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
55
56
'use strict';
const mms = require('mongodb-memory-server');
/*
* Default MMS mongodb version is used, unless MONGOMS_VERSION is set (which is set with the matrix in test.yml for CI)
*/
// a single-instance running for single-instance tests
let mongoinstance;
// a replset-instance for running repl-set tests
let mongorreplset;
// decide wheter to start a in-memory or not
const startMemoryInstance = !process.env.MONGOOSE_TEST_URI;
const startMemoryReplset = !process.env.MONGOOSE_REPLSET_URI;
module.exports.mochaGlobalSetup = async function mochaGlobalSetup() {
let instanceuri;
let replseturi;
process.env.RUNTIME_DOWNLOAD = '1'; // ensure MMS is able to download binaries in this context
// set some options when running in a CI
if (process.env.CI) {
process.env.MONGOMS_PREFER_GLOBAL_PATH = '1'; // set MMS to use "~/.cache/mongodb-binaries" even when the path does not yet exist
}
if (startMemoryInstance) { // Config to decided if an mongodb-memory-server instance should be used
// it's needed in global space, because we don't want to create a new instance every test-suite
mongoinstance = await mms.MongoMemoryServer.create({ instance: { args: ['--setParameter', 'ttlMonitorSleepSecs=1'], storageEngine: 'wiredTiger' } });
instanceuri = mongoinstance.getUri();
} else {
instanceuri = process.env.MONGOOSE_TEST_URI;
}
if (startMemoryReplset) {
mongorreplset = await mms.MongoMemoryReplSet.create({ replSet: { count: 3, args: ['--setParameter', 'ttlMonitorSleepSecs=1'], storageEngine: 'wiredTiger' } }); // using 3 because even numbers can lead to vote problems
replseturi = mongorreplset.getUri();
} else {
replseturi = '';
}
process.env.MONGOOSE_TEST_URI = instanceuri;
process.env.MONGOOSE_REPLSET_URI = replseturi;
};
module.exports.mochaGlobalTeardown = async function mochaGlobalTeardown() {
if (mongoinstance) { // Config to decided if an mongodb-memory-server instance should be used
await mongoinstance.stop();
}
if (mongorreplset) {
await mongorreplset.stop();
}
};