Skip to content

Commit c9d3f6a

Browse files
authored
Merge pull request #458 from shelfio/feature/fix-jest-mongodb-config-location
Config locaiton improvements
2 parents 6ff4475 + 91d12e3 commit c9d3f6a

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "@shelf/jest-mongodb",
3-
"version": "4.2.0",
4-
"private": false,
3+
"version": "4.3.0",
54
"description": "Run your tests using Jest & MongoDB in Memory server",
65
"keywords": [
76
"jest",
@@ -63,6 +62,7 @@
6362
"eslint": "8.53.0",
6463
"husky": "8.0.3",
6564
"jest": "29.7.0",
65+
"jest-environment-node": "29.6.4",
6666
"lint-staged": "13.3.0",
6767
"mongodb": "6.3.0",
6868
"prettier": "2.8.8",

src/environment.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import {getMongodbMemoryOptions} from './helpers';
1010
// eslint-disable-next-line import/order
1111
const debug = require('debug')('jest-mongodb:environment');
1212

13-
const options = getMongodbMemoryOptions();
14-
const isReplSet = Boolean(options.replSet);
15-
16-
debug(`isReplSet`, isReplSet);
17-
18-
const mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
19-
2013
module.exports = class MongoEnvironment extends TestEnvironment {
2114
globalConfigPath: string;
15+
mongo: MongoMemoryReplSet | MongoMemoryServer;
2216
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
2317
super(config, context);
2418
this.globalConfigPath = pathJoin(config.globalConfig.rootDir, 'globalConfig.json');
19+
20+
const options = getMongodbMemoryOptions(config.globalConfig.rootDir);
21+
const isReplSet = Boolean(options.replSet);
22+
debug(`isReplSet`, isReplSet);
23+
24+
this.mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
2525
}
2626

2727
async setup() {
@@ -32,9 +32,9 @@ module.exports = class MongoEnvironment extends TestEnvironment {
3232
if (globalConfig.mongoUri) {
3333
this.global.__MONGO_URI__ = globalConfig.mongoUri;
3434
} else {
35-
await mongo.start();
35+
await this.mongo.start();
3636

37-
this.global.__MONGO_URI__ = mongo.getUri();
37+
this.global.__MONGO_URI__ = this.mongo.getUri();
3838
}
3939

4040
this.global.__MONGO_DB_NAME__ = globalConfig.mongoDBName || randomUUID();
@@ -45,7 +45,7 @@ module.exports = class MongoEnvironment extends TestEnvironment {
4545
async teardown() {
4646
debug('Teardown MongoDB Test Environment');
4747

48-
await mongo.stop();
48+
await this.mongo.stop();
4949

5050
await super.teardown();
5151
}

src/helpers.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {resolve} from 'path';
22

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

6-
export function getMongodbMemoryOptions() {
5+
export function getMongodbMemoryOptions(cwd: string) {
76
try {
87
const {mongodbMemoryServerOptions} = require(resolve(cwd, configFile));
98

@@ -19,7 +18,7 @@ export function getMongodbMemoryOptions() {
1918
}
2019
}
2120

22-
export function getMongoURLEnvName() {
21+
export function getMongoURLEnvName(cwd: string) {
2322
try {
2423
const {mongoURLEnvName} = require(resolve(cwd, configFile));
2524

@@ -29,7 +28,7 @@ export function getMongoURLEnvName() {
2928
}
3029
}
3130

32-
export function shouldUseSharedDBForAllJestWorkers() {
31+
export function shouldUseSharedDBForAllJestWorkers(cwd: string) {
3332
try {
3433
const {useSharedDBForAllJestWorkers} = require(resolve(cwd, configFile));
3534

src/setup.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,34 @@ import {
1111
} from './helpers';
1212

1313
const debug = require('debug')('jest-mongodb:setup');
14-
const mongoMemoryServerOptions = getMongodbMemoryOptions();
15-
const isReplSet = Boolean(mongoMemoryServerOptions.replSet);
16-
17-
debug(`isReplSet ${isReplSet}`);
18-
19-
// @ts-ignore
20-
const mongo: Mongo = isReplSet
21-
? new MongoMemoryReplSet(mongoMemoryServerOptions)
22-
: new MongoMemoryServer(mongoMemoryServerOptions);
2314

2415
module.exports = async (config: JestEnvironmentConfig['globalConfig']) => {
2516
const globalConfigPath = join(config.rootDir, 'globalConfig.json');
2617

27-
const options = getMongodbMemoryOptions();
18+
const mongoMemoryServerOptions = getMongodbMemoryOptions(config.rootDir);
19+
const isReplSet = Boolean(mongoMemoryServerOptions.replSet);
20+
21+
debug(`isReplSet ${isReplSet}`);
22+
23+
// @ts-ignore
24+
const mongo: Mongo = isReplSet
25+
? new MongoMemoryReplSet(mongoMemoryServerOptions)
26+
: new MongoMemoryServer(mongoMemoryServerOptions);
27+
28+
const options = getMongodbMemoryOptions(config.rootDir);
2829
const mongoConfig: {mongoUri?: string; mongoDBName?: string} = {};
2930

30-
debug(`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers()}`);
31+
debug(
32+
`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers(config.rootDir)}`
33+
);
3134

3235
// if we run one mongodb instance for all tests
33-
if (shouldUseSharedDBForAllJestWorkers()) {
36+
if (shouldUseSharedDBForAllJestWorkers(config.rootDir)) {
3437
if (!mongo.isRunning) {
3538
await mongo.start();
3639
}
3740

38-
const mongoURLEnvName = getMongoURLEnvName();
41+
const mongoURLEnvName = getMongoURLEnvName(config.rootDir);
3942

4043
mongoConfig.mongoUri = await mongo.getUri();
4144

0 commit comments

Comments
 (0)