diff --git a/package.json b/package.json index 08936304..6bab6156 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/environment.ts b/src/environment.ts index 5271f4d2..b6d7dd6e 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -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() { @@ -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(); @@ -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(); } diff --git a/src/helpers.ts b/src/helpers.ts index 011b74a6..09004ba0 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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)); @@ -19,7 +18,7 @@ export function getMongodbMemoryOptions() { } } -export function getMongoURLEnvName() { +export function getMongoURLEnvName(cwd: string) { try { const {mongoURLEnvName} = require(resolve(cwd, configFile)); @@ -29,7 +28,7 @@ export function getMongoURLEnvName() { } } -export function shouldUseSharedDBForAllJestWorkers() { +export function shouldUseSharedDBForAllJestWorkers(cwd: string) { try { const {useSharedDBForAllJestWorkers} = require(resolve(cwd, configFile)); diff --git a/src/setup.ts b/src/setup.ts index 95928bf8..67b6b64d 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -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();