Skip to content

Commit d8bdd7f

Browse files
authored
Merge pull request #320 from arisjulio/replset-support
2 parents b6fc0ed + 6b43b1c commit d8bdd7f

File tree

8 files changed

+59
-13
lines changed

8 files changed

+59
-13
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ jobs:
2424
key: v2-dependencies-{{ checksum "package.json" }}
2525

2626
- run: yarn test
27+
- run: yarn test:repl

environment.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ const NodeEnvironment = require('jest-environment-node');
22
const path = require('path');
33
const fs = require('fs');
44
const uuid = require('uuid');
5-
const {MongoMemoryServer} = require('mongodb-memory-server');
5+
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
66
const {getMongodbMemoryOptions} = require('./helpers');
77

88
const debug = require('debug')('jest-mongodb:environment');
99

1010
const cwd = process.cwd();
1111

1212
const globalConfigPath = path.join(cwd, 'globalConfig.json');
13+
const options = getMongodbMemoryOptions();
14+
const isReplSet = Boolean(options.replSet);
1315

14-
let mongo = new MongoMemoryServer(getMongodbMemoryOptions());
16+
debug(`isReplSet`, isReplSet);
17+
18+
let mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
1519

1620
module.exports = class MongoEnvironment extends NodeEnvironment {
1721
constructor(config, context) {

helpers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const {resolve} = require('path');
22

33
const cwd = process.cwd();
4+
const configFile = process.env.MONGO_MEMORY_SERVER_FILE || 'jest-mongodb-config.js';
45

56
module.exports.getMongodbMemoryOptions = function () {
67
try {
7-
const {mongodbMemoryServerOptions} = require(resolve(cwd, 'jest-mongodb-config.js'));
8+
const {mongodbMemoryServerOptions} = require(resolve(cwd, configFile));
89

910
return mongodbMemoryServerOptions;
1011
} catch (e) {
@@ -20,7 +21,7 @@ module.exports.getMongodbMemoryOptions = function () {
2021

2122
module.exports.getMongoURLEnvName = function () {
2223
try {
23-
const {mongoURLEnvName} = require(resolve(cwd, 'jest-mongodb-config.js'));
24+
const {mongoURLEnvName} = require(resolve(cwd, configFile));
2425

2526
return mongoURLEnvName || 'MONGO_URL';
2627
} catch (e) {
@@ -30,7 +31,7 @@ module.exports.getMongoURLEnvName = function () {
3031

3132
module.exports.shouldUseSharedDBForAllJestWorkers = function () {
3233
try {
33-
const {useSharedDBForAllJestWorkers} = require(resolve(cwd, 'jest-mongodb-config.js'));
34+
const {useSharedDBForAllJestWorkers} = require(resolve(cwd, configFile));
3435

3536
if (typeof useSharedDBForAllJestWorkers === 'undefined') {
3637
return true;

jest-mongodb-config-repl.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
mongodbMemoryServerOptions: {
3+
binary: {
4+
skipMD5: true,
5+
},
6+
autoStart: false,
7+
instance: {},
8+
replSet: {
9+
count: 4,
10+
storageEngine: 'wiredTiger',
11+
},
12+
},
13+
mongoURLEnvName: 'MONGO_URL',
14+
};

mongo-insert.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ describe('insert', () => {
55
let connection;
66
let db;
77

8-
98
beforeAll(async () => {
109
connection = await MongoClient.connect(uri, {
1110
useNewUrlParser: true,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
"scripts": {
2121
"lint": "eslint . --fix --ext .js,.json,.ts --quiet",
22-
"test": "jest"
22+
"test": "jest",
23+
"test:repl": "MONGO_MEMORY_SERVER_FILE=jest-mongodb-config-repl.js jest"
2324
},
2425
"husky": {
2526
"hooks": {

readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ module.exports = {
103103
};
104104
```
105105

106+
To use mongo as a replica set you must add the `replSet` config object and set
107+
`count` and `storageEngine` fields:
108+
109+
```js
110+
module.exports = {
111+
mongodbMemoryServerOptions: {
112+
binary: {
113+
skipMD5: true,
114+
},
115+
autoStart: false,
116+
instance: {},
117+
replSet: {
118+
count: 3,
119+
storageEngine: 'wiredTiger',
120+
},
121+
},
122+
};
123+
```
124+
106125
### 3. Configure MongoDB client
107126

108127
Library sets the `process.env.MONGO_URL` for your convenience, but using of `global.__MONGO_URI__` is preferable as it works with ` useSharedDBForAllJestWorkers: false`

setup.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
const fs = require('fs');
22
const {join} = require('path');
3-
const {MongoMemoryServer} = require('mongodb-memory-server');
3+
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
44
const {
55
getMongodbMemoryOptions,
66
getMongoURLEnvName,
77
shouldUseSharedDBForAllJestWorkers,
88
} = require('./helpers');
99

1010
const debug = require('debug')('jest-mongodb:setup');
11-
const mongod = new MongoMemoryServer(getMongodbMemoryOptions());
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);
1219

1320
const cwd = process.cwd();
1421
const globalConfigPath = join(cwd, 'globalConfig.json');
@@ -21,18 +28,18 @@ module.exports = async () => {
2128

2229
// if we run one mongodb instance for all tests
2330
if (shouldUseSharedDBForAllJestWorkers()) {
24-
if (!mongod.isRunning) {
25-
await mongod.start();
31+
if (!mongo.isRunning) {
32+
await mongo.start();
2633
}
2734

2835
const mongoURLEnvName = getMongoURLEnvName();
2936

30-
mongoConfig.mongoUri = await mongod.getUri();
37+
mongoConfig.mongoUri = await mongo.getUri();
3138

3239
process.env[mongoURLEnvName] = mongoConfig.mongoUri;
3340

3441
// Set reference to mongod in order to close the server during teardown.
35-
global.__MONGOD__ = mongod;
42+
global.__MONGOD__ = mongo;
3643
}
3744

3845
mongoConfig.mongoDBName = options.instance.dbName;

0 commit comments

Comments
 (0)