From 6346fd003b41c3a897ee24417154486c02eeb493 Mon Sep 17 00:00:00 2001 From: Adam Wootton Date: Fri, 6 Jan 2023 12:24:05 -0500 Subject: [PATCH 1/2] fix global setup and teardown for jest multi-project setups --- src/setup.ts | 10 +++++++--- src/teardown.ts | 22 +++++++++++++--------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/setup.ts b/src/setup.ts index 2b38e9a1..aaaf8441 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -21,8 +21,9 @@ const mongo: Mongo = isReplSet ? new MongoMemoryReplSet(mongoMemoryServerOptions) : new MongoMemoryServer(mongoMemoryServerOptions); -module.exports = async (config: JestEnvironmentConfig['projectConfig']) => { - const globalConfigPath = join(config.rootDir, 'globalConfig.json'); +module.exports = async (config: JestEnvironmentConfig['globalConfig']) => { + const projects = config.projects.length ? config.projects : [config.rootDir]; + const globalConfigPaths = projects.map(project => join(project, 'globalConfig.json')); const options = getMongodbMemoryOptions(); const mongoConfig: {mongoUri?: string; mongoDBName?: string} = {}; @@ -48,6 +49,9 @@ module.exports = async (config: JestEnvironmentConfig['projectConfig']) => { mongoConfig.mongoDBName = options.instance.dbName; // Write global config to disk because all tests run in different contexts. - writeFileSync(globalConfigPath, JSON.stringify(mongoConfig)); + // write one for each registered "project" + for (const path of globalConfigPaths) { + writeFileSync(path, JSON.stringify(mongoConfig)); + } debug('Config is written'); }; diff --git a/src/teardown.ts b/src/teardown.ts index a7ca4c57..e6db388e 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -4,19 +4,23 @@ import type {JestEnvironmentConfig} from '@jest/environment'; const debug = require('debug')('jest-mongodb:teardown'); -module.exports = async function (config: JestEnvironmentConfig['projectConfig']) { - const globalConfigPath = join(config.rootDir, 'globalConfig.json'); +module.exports = async function (config: JestEnvironmentConfig['globalConfig']) { + const projects = config.projects.length ? config.projects : [config.rootDir]; + const globalConfigPaths = projects.map(project => join(project, 'globalConfig.json')); debug('Teardown mongod'); if (global.__MONGOD__) { await global.__MONGOD__.stop(); } - unlink(globalConfigPath, err => { - if (err) { - debug('Config could not be deleted'); - return; - } - debug('Config is deleted'); - }); + for (const path of globalConfigPaths) { + unlink(path, err => { + if (err) { + debug('Config could not be deleted'); + + return; + } + debug('Config is deleted'); + }); + } }; From 42faf860440e6d62d401d804b261d70233b2a7f7 Mon Sep 17 00:00:00 2001 From: Adam Wootton Date: Mon, 9 Jan 2023 14:12:23 -0500 Subject: [PATCH 2/2] switch to using global config's rootDir field --- src/environment.ts | 2 +- src/setup.ts | 8 ++------ src/teardown.ts | 20 ++++++++------------ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/environment.ts b/src/environment.ts index 4e8e1c6b..2d64ecc9 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -21,7 +21,7 @@ module.exports = class MongoEnvironment extends TestEnvironment { globalConfigPath: string; constructor(config: JestEnvironmentConfig, context: EnvironmentContext) { super(config, context); - this.globalConfigPath = pathJoin(config.projectConfig.rootDir, 'globalConfig.json'); + this.globalConfigPath = pathJoin(config.globalConfig.rootDir, 'globalConfig.json'); } async setup() { diff --git a/src/setup.ts b/src/setup.ts index aaaf8441..c5832e96 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -22,8 +22,7 @@ const mongo: Mongo = isReplSet : new MongoMemoryServer(mongoMemoryServerOptions); module.exports = async (config: JestEnvironmentConfig['globalConfig']) => { - const projects = config.projects.length ? config.projects : [config.rootDir]; - const globalConfigPaths = projects.map(project => join(project, 'globalConfig.json')); + const globalConfigPath = join(config.rootDir, 'globalConfig.json'); const options = getMongodbMemoryOptions(); const mongoConfig: {mongoUri?: string; mongoDBName?: string} = {}; @@ -49,9 +48,6 @@ module.exports = async (config: JestEnvironmentConfig['globalConfig']) => { mongoConfig.mongoDBName = options.instance.dbName; // Write global config to disk because all tests run in different contexts. - // write one for each registered "project" - for (const path of globalConfigPaths) { - writeFileSync(path, JSON.stringify(mongoConfig)); - } + writeFileSync(globalConfigPath, JSON.stringify(mongoConfig)); debug('Config is written'); }; diff --git a/src/teardown.ts b/src/teardown.ts index e6db388e..8dab3538 100644 --- a/src/teardown.ts +++ b/src/teardown.ts @@ -5,22 +5,18 @@ import type {JestEnvironmentConfig} from '@jest/environment'; const debug = require('debug')('jest-mongodb:teardown'); module.exports = async function (config: JestEnvironmentConfig['globalConfig']) { - const projects = config.projects.length ? config.projects : [config.rootDir]; - const globalConfigPaths = projects.map(project => join(project, 'globalConfig.json')); + const globalConfigPath = join(config.rootDir, 'globalConfig.json'); debug('Teardown mongod'); if (global.__MONGOD__) { await global.__MONGOD__.stop(); } + unlink(globalConfigPath, err => { + if (err) { + debug('Config could not be deleted'); - for (const path of globalConfigPaths) { - unlink(path, err => { - if (err) { - debug('Config could not be deleted'); - - return; - } - debug('Config is deleted'); - }); - } + return; + } + debug('Config is deleted'); + }); };