Skip to content

Commit

Permalink
feat: Fix jest-mongodb-config location issue by using Jest's globalCo…
Browse files Browse the repository at this point in the history
…nfig for cwd in monorepo tests and add jest-environment-node devDependency (by @JamieSinn)
  • Loading branch information
harazdovskiy committed May 2, 2024
1 parent 6ff4475 commit 91d12e3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "@shelf/jest-mongodb",
"version": "4.2.0",
"private": false,
"version": "4.3.0",
"description": "Run your tests using Jest & MongoDB in Memory server",
"keywords": [
"jest",
Expand Down Expand Up @@ -63,6 +62,7 @@
"eslint": "8.53.0",
"husky": "8.0.3",
"jest": "29.7.0",
"jest-environment-node": "29.6.4",
"lint-staged": "13.3.0",
"mongodb": "6.3.0",
"prettier": "2.8.8",
Expand Down
20 changes: 10 additions & 10 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import {getMongodbMemoryOptions} from './helpers';
// eslint-disable-next-line import/order
const debug = require('debug')('jest-mongodb:environment');

const options = getMongodbMemoryOptions();
const isReplSet = Boolean(options.replSet);

debug(`isReplSet`, isReplSet);

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

module.exports = class MongoEnvironment extends TestEnvironment {
globalConfigPath: string;
mongo: MongoMemoryReplSet | MongoMemoryServer;
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
super(config, context);
this.globalConfigPath = pathJoin(config.globalConfig.rootDir, 'globalConfig.json');

const options = getMongodbMemoryOptions(config.globalConfig.rootDir);
const isReplSet = Boolean(options.replSet);
debug(`isReplSet`, isReplSet);

this.mongo = isReplSet ? new MongoMemoryReplSet(options) : new MongoMemoryServer(options);
}

async setup() {
Expand All @@ -32,9 +32,9 @@ module.exports = class MongoEnvironment extends TestEnvironment {
if (globalConfig.mongoUri) {
this.global.__MONGO_URI__ = globalConfig.mongoUri;
} else {
await mongo.start();
await this.mongo.start();

this.global.__MONGO_URI__ = mongo.getUri();
this.global.__MONGO_URI__ = this.mongo.getUri();
}

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

await mongo.stop();
await this.mongo.stop();

await super.teardown();
}
Expand Down
7 changes: 3 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {resolve} from 'path';

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

export function getMongodbMemoryOptions() {
export function getMongodbMemoryOptions(cwd: string) {
try {
const {mongodbMemoryServerOptions} = require(resolve(cwd, configFile));

Expand All @@ -19,7 +18,7 @@ export function getMongodbMemoryOptions() {
}
}

export function getMongoURLEnvName() {
export function getMongoURLEnvName(cwd: string) {
try {
const {mongoURLEnvName} = require(resolve(cwd, configFile));

Expand All @@ -29,7 +28,7 @@ export function getMongoURLEnvName() {
}
}

export function shouldUseSharedDBForAllJestWorkers() {
export function shouldUseSharedDBForAllJestWorkers(cwd: string) {
try {
const {useSharedDBForAllJestWorkers} = require(resolve(cwd, configFile));

Expand Down
29 changes: 16 additions & 13 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@ import {
} from './helpers';

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

debug(`isReplSet ${isReplSet}`);

// @ts-ignore
const mongo: Mongo = isReplSet
? new MongoMemoryReplSet(mongoMemoryServerOptions)
: new MongoMemoryServer(mongoMemoryServerOptions);

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

const options = getMongodbMemoryOptions();
const mongoMemoryServerOptions = getMongodbMemoryOptions(config.rootDir);
const isReplSet = Boolean(mongoMemoryServerOptions.replSet);

debug(`isReplSet ${isReplSet}`);

// @ts-ignore
const mongo: Mongo = isReplSet
? new MongoMemoryReplSet(mongoMemoryServerOptions)
: new MongoMemoryServer(mongoMemoryServerOptions);

const options = getMongodbMemoryOptions(config.rootDir);
const mongoConfig: {mongoUri?: string; mongoDBName?: string} = {};

debug(`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers()}`);
debug(
`shouldUseSharedDBForAllJestWorkers: ${shouldUseSharedDBForAllJestWorkers(config.rootDir)}`
);

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

const mongoURLEnvName = getMongoURLEnvName();
const mongoURLEnvName = getMongoURLEnvName(config.rootDir);

mongoConfig.mongoUri = await mongo.getUri();

Expand Down

0 comments on commit 91d12e3

Please sign in to comment.