Skip to content

Commit

Permalink
Merge pull request #320 from arisjulio/replset-support
Browse files Browse the repository at this point in the history
  • Loading branch information
vladgolubev committed Dec 28, 2021
2 parents b6fc0ed + 6b43b1c commit d8bdd7f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 13 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ jobs:
key: v2-dependencies-{{ checksum "package.json" }}

- run: yarn test
- run: yarn test:repl
8 changes: 6 additions & 2 deletions environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ const NodeEnvironment = require('jest-environment-node');
const path = require('path');
const fs = require('fs');
const uuid = require('uuid');
const {MongoMemoryServer} = require('mongodb-memory-server');
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);

let mongo = new MongoMemoryServer(getMongodbMemoryOptions());
debug(`isReplSet`, isReplSet);

let mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);

module.exports = class MongoEnvironment extends NodeEnvironment {
constructor(config, context) {
Expand Down
7 changes: 4 additions & 3 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const {resolve} = require('path');

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

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

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

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

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

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

if (typeof useSharedDBForAllJestWorkers === 'undefined') {
return true;
Expand Down
14 changes: 14 additions & 0 deletions jest-mongodb-config-repl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
mongodbMemoryServerOptions: {
binary: {
skipMD5: true,
},
autoStart: false,
instance: {},
replSet: {
count: 4,
storageEngine: 'wiredTiger',
},
},
mongoURLEnvName: 'MONGO_URL',
};
1 change: 0 additions & 1 deletion mongo-insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('insert', () => {
let connection;
let db;


beforeAll(async () => {
connection = await MongoClient.connect(uri, {
useNewUrlParser: true,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
},
"scripts": {
"lint": "eslint . --fix --ext .js,.json,.ts --quiet",
"test": "jest"
"test": "jest",
"test:repl": "MONGO_MEMORY_SERVER_FILE=jest-mongodb-config-repl.js jest"
},
"husky": {
"hooks": {
Expand Down
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ module.exports = {
};
```

To use mongo as a replica set you must add the `replSet` config object and set
`count` and `storageEngine` fields:

```js
module.exports = {
mongodbMemoryServerOptions: {
binary: {
skipMD5: true,
},
autoStart: false,
instance: {},
replSet: {
count: 3,
storageEngine: 'wiredTiger',
},
},
};
```

### 3. Configure MongoDB client

Library sets the `process.env.MONGO_URL` for your convenience, but using of `global.__MONGO_URI__` is preferable as it works with ` useSharedDBForAllJestWorkers: false`
Expand Down
19 changes: 13 additions & 6 deletions setup.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
const fs = require('fs');
const {join} = require('path');
const {MongoMemoryServer} = require('mongodb-memory-server');
const {MongoMemoryServer, MongoMemoryReplSet} = require('mongodb-memory-server');
const {
getMongodbMemoryOptions,
getMongoURLEnvName,
shouldUseSharedDBForAllJestWorkers,
} = require('./helpers');

const debug = require('debug')('jest-mongodb:setup');
const mongod = new MongoMemoryServer(getMongodbMemoryOptions());
const mongoMemoryServerOptions = getMongodbMemoryOptions();
const isReplSet = Boolean(mongoMemoryServerOptions.replSet);

debug(`isReplSet ${isReplSet}`);

const mongo = isReplSet
? new MongoMemoryReplSet(mongoMemoryServerOptions)
: new MongoMemoryServer(mongoMemoryServerOptions);

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

// if we run one mongodb instance for all tests
if (shouldUseSharedDBForAllJestWorkers()) {
if (!mongod.isRunning) {
await mongod.start();
if (!mongo.isRunning) {
await mongo.start();
}

const mongoURLEnvName = getMongoURLEnvName();

mongoConfig.mongoUri = await mongod.getUri();
mongoConfig.mongoUri = await mongo.getUri();

process.env[mongoURLEnvName] = mongoConfig.mongoUri;

// Set reference to mongod in order to close the server during teardown.
global.__MONGOD__ = mongod;
global.__MONGOD__ = mongo;
}

mongoConfig.mongoDBName = options.instance.dbName;
Expand Down

0 comments on commit d8bdd7f

Please sign in to comment.