From 7fd725946455a5589968707b9bb17b6376b415c2 Mon Sep 17 00:00:00 2001 From: Jan W Date: Thu, 3 Oct 2024 17:14:25 +0200 Subject: [PATCH 1/6] fix(jest): support ESM setup files --- jest.config.mjs | 5 +++++ src/jest.config.js | 8 ++++++-- tests/setup-files/index.test.js | 11 +++++++++++ tests/setup-files/setup.cjs | 3 +++ tests/setup-files/setup.js | 3 +++ tests/setup-files/setup.mjs | 3 +++ 6 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/setup-files/index.test.js create mode 100644 tests/setup-files/setup.cjs create mode 100644 tests/setup-files/setup.js create mode 100644 tests/setup-files/setup.mjs diff --git a/jest.config.mjs b/jest.config.mjs index e069ddd..8138a72 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -1,5 +1,10 @@ const config = { testMatch: [`**/?(*.)+(spec|test).?([cm])[jt]s?(x)`], // From bin/index.js defaults + setupFiles: [ + '/tests/setup-files/setup.cjs', + '/tests/setup-files/setup.mjs', + '/tests/setup-files/setup.js', + ] } export default config diff --git a/src/jest.config.js b/src/jest.config.js index 9b6f55f..73ac585 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -102,6 +102,8 @@ export async function installJestEnvironment(jestGlobals) { if (c.resetModules) beforeEach(() => jest.resetModules()) let require + let dynamicImport + if (process.env.EXODUS_TEST_ENVIRONMENT === 'bundle') { const preloaded = new Map(EXODUS_TEST_PRELOADED) // eslint-disable-line no-undef require = (name) => { @@ -112,18 +114,20 @@ export async function installJestEnvironment(jestGlobals) { const { resolve } = await import('node:path') const { createRequire } = await import('node:module') require = createRequire(resolve(config.rootDir, 'package.json')) + dynamicImport = (path) => import(resolve(config.rootDir, path)) } else { require = () => assert.fail('Unreachable: requiring plugins without a rootDir') + dynamicImport = () => assert.fail('Unreachable: importing plugins without a rootDir') } - for (const file of c.setupFiles || []) require(file) + for (const file of c.setupFiles || []) await dynamicImport(file) if (Object.hasOwn(specialEnvironments, c.testEnvironment)) { const { setup } = specialEnvironments[c.testEnvironment] await setup(require, engine, jestGlobals, c.testEnvironmentOptions) } - for (const file of c.setupFilesAfterEnv || []) require(file) + for (const file of c.setupFilesAfterEnv || []) await dynamicImport(file) // @jest/globals import auto-mocking is disabled until https://github.com/nodejs/node/issues/53807 is resolved /* diff --git a/tests/setup-files/index.test.js b/tests/setup-files/index.test.js new file mode 100644 index 0000000..1af8c19 --- /dev/null +++ b/tests/setup-files/index.test.js @@ -0,0 +1,11 @@ +test('imports .cjs file', () => { + expect(global.SETUP_CJS).toBe('setup.cjs') +}) + +test('imports .mjs file', () => { + expect(global.SETUP_MJS).toBe('setup.mjs') +}) + +test('imports .js (module)', () => { + expect(global.SETUP_JS_MODULE).toBe('setup.js') +}) \ No newline at end of file diff --git a/tests/setup-files/setup.cjs b/tests/setup-files/setup.cjs new file mode 100644 index 0000000..62cc1b3 --- /dev/null +++ b/tests/setup-files/setup.cjs @@ -0,0 +1,3 @@ +const path = require('path') + +global.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works \ No newline at end of file diff --git a/tests/setup-files/setup.js b/tests/setup-files/setup.js new file mode 100644 index 0000000..83d4efa --- /dev/null +++ b/tests/setup-files/setup.js @@ -0,0 +1,3 @@ +const { default: path } = await import('path') + +global.SETUP_JS_MODULE = path.basename(import.meta.url) \ No newline at end of file diff --git a/tests/setup-files/setup.mjs b/tests/setup-files/setup.mjs new file mode 100644 index 0000000..3b51a17 --- /dev/null +++ b/tests/setup-files/setup.mjs @@ -0,0 +1,3 @@ +import path from 'path' + +global.SETUP_MJS = path.basename(import.meta.url) \ No newline at end of file From b2c14d3c6579875f2b9bf830f5dddcef1adc08ef Mon Sep 17 00:00:00 2001 From: Jan W Date: Thu, 3 Oct 2024 17:15:32 +0200 Subject: [PATCH 2/6] chore: lint --- jest.config.mjs | 2 +- tests/setup-files/index.test.js | 2 +- tests/setup-files/setup.cjs | 2 +- tests/setup-files/setup.js | 2 +- tests/setup-files/setup.mjs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/jest.config.mjs b/jest.config.mjs index 8138a72..f5d64da 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -4,7 +4,7 @@ const config = { '/tests/setup-files/setup.cjs', '/tests/setup-files/setup.mjs', '/tests/setup-files/setup.js', - ] + ], } export default config diff --git a/tests/setup-files/index.test.js b/tests/setup-files/index.test.js index 1af8c19..f9a4cee 100644 --- a/tests/setup-files/index.test.js +++ b/tests/setup-files/index.test.js @@ -8,4 +8,4 @@ test('imports .mjs file', () => { test('imports .js (module)', () => { expect(global.SETUP_JS_MODULE).toBe('setup.js') -}) \ No newline at end of file +}) diff --git a/tests/setup-files/setup.cjs b/tests/setup-files/setup.cjs index 62cc1b3..6d506f2 100644 --- a/tests/setup-files/setup.cjs +++ b/tests/setup-files/setup.cjs @@ -1,3 +1,3 @@ const path = require('path') -global.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works \ No newline at end of file +global.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works diff --git a/tests/setup-files/setup.js b/tests/setup-files/setup.js index 83d4efa..ccd3f4a 100644 --- a/tests/setup-files/setup.js +++ b/tests/setup-files/setup.js @@ -1,3 +1,3 @@ const { default: path } = await import('path') -global.SETUP_JS_MODULE = path.basename(import.meta.url) \ No newline at end of file +global.SETUP_JS_MODULE = path.basename(import.meta.url) diff --git a/tests/setup-files/setup.mjs b/tests/setup-files/setup.mjs index 3b51a17..f4904a3 100644 --- a/tests/setup-files/setup.mjs +++ b/tests/setup-files/setup.mjs @@ -1,3 +1,3 @@ import path from 'path' -global.SETUP_MJS = path.basename(import.meta.url) \ No newline at end of file +global.SETUP_MJS = path.basename(import.meta.url) From 6ce4c3ad52e823246cae9bea6a6e274f7648dec1 Mon Sep 17 00:00:00 2001 From: Jan W Date: Thu, 3 Oct 2024 17:26:19 +0200 Subject: [PATCH 3/6] fix: hermes tests --- src/jest.config.js | 2 +- tests/setup-files/setup.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jest.config.js b/src/jest.config.js index 73ac585..dd0ca6e 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -106,7 +106,7 @@ export async function installJestEnvironment(jestGlobals) { if (process.env.EXODUS_TEST_ENVIRONMENT === 'bundle') { const preloaded = new Map(EXODUS_TEST_PRELOADED) // eslint-disable-line no-undef - require = (name) => { + require = dynamicImport = (name) => { if (preloaded.has(name)) return preloaded.get(name)() assert.fail('Requiring non-bundled plugins from bundle is unsupported') } diff --git a/tests/setup-files/setup.js b/tests/setup-files/setup.js index ccd3f4a..c7234c2 100644 --- a/tests/setup-files/setup.js +++ b/tests/setup-files/setup.js @@ -1,3 +1,3 @@ -const { default: path } = await import('path') +import path from 'path' global.SETUP_JS_MODULE = path.basename(import.meta.url) From 0b713d3048e765a3a477095c1fff6de3792fc618 Mon Sep 17 00:00:00 2001 From: Jan W Date: Thu, 3 Oct 2024 17:43:02 +0200 Subject: [PATCH 4/6] refactor: kill require --- src/jest.config.js | 9 ++------- src/jest.environment.js | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/jest.config.js b/src/jest.config.js index dd0ca6e..a7144c8 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -101,22 +101,17 @@ export async function installJestEnvironment(jestGlobals) { if (c.restoreMocks) beforeEach(() => jest.restoreAllMocks()) if (c.resetModules) beforeEach(() => jest.resetModules()) - let require let dynamicImport - if (process.env.EXODUS_TEST_ENVIRONMENT === 'bundle') { const preloaded = new Map(EXODUS_TEST_PRELOADED) // eslint-disable-line no-undef - require = dynamicImport = (name) => { + dynamicImport = (name) => { if (preloaded.has(name)) return preloaded.get(name)() assert.fail('Requiring non-bundled plugins from bundle is unsupported') } } else if (config.rootDir) { const { resolve } = await import('node:path') - const { createRequire } = await import('node:module') - require = createRequire(resolve(config.rootDir, 'package.json')) dynamicImport = (path) => import(resolve(config.rootDir, path)) } else { - require = () => assert.fail('Unreachable: requiring plugins without a rootDir') dynamicImport = () => assert.fail('Unreachable: importing plugins without a rootDir') } @@ -124,7 +119,7 @@ export async function installJestEnvironment(jestGlobals) { if (Object.hasOwn(specialEnvironments, c.testEnvironment)) { const { setup } = specialEnvironments[c.testEnvironment] - await setup(require, engine, jestGlobals, c.testEnvironmentOptions) + await setup(dynamicImport, engine, jestGlobals, c.testEnvironmentOptions) } for (const file of c.setupFilesAfterEnv || []) await dynamicImport(file) diff --git a/src/jest.environment.js b/src/jest.environment.js index 3229bb1..4f9bc5b 100644 --- a/src/jest.environment.js +++ b/src/jest.environment.js @@ -3,8 +3,8 @@ export const specialEnvironments = { jsdom: { dependencies: ['jsdom'], - setup: (require) => { - const { JSDOM, VirtualConsole } = require('jsdom') + setup: async (dynamicImport) => { + const { JSDOM, VirtualConsole } = await dynamicImport('jsdom') const virtualConsole = new VirtualConsole() const dom = new JSDOM('', { url: 'http://localhost/', From d6aee79a43e94164624149306e5c642116140509 Mon Sep 17 00:00:00 2001 From: Jan W Date: Thu, 3 Oct 2024 17:43:41 +0200 Subject: [PATCH 5/6] fix: make sure it's async --- src/jest.config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jest.config.js b/src/jest.config.js index a7144c8..46792fb 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -104,7 +104,7 @@ export async function installJestEnvironment(jestGlobals) { let dynamicImport if (process.env.EXODUS_TEST_ENVIRONMENT === 'bundle') { const preloaded = new Map(EXODUS_TEST_PRELOADED) // eslint-disable-line no-undef - dynamicImport = (name) => { + dynamicImport = async (name) => { if (preloaded.has(name)) return preloaded.get(name)() assert.fail('Requiring non-bundled plugins from bundle is unsupported') } @@ -112,7 +112,7 @@ export async function installJestEnvironment(jestGlobals) { const { resolve } = await import('node:path') dynamicImport = (path) => import(resolve(config.rootDir, path)) } else { - dynamicImport = () => assert.fail('Unreachable: importing plugins without a rootDir') + dynamicImport = async () => assert.fail('Unreachable: importing plugins without a rootDir') } for (const file of c.setupFiles || []) await dynamicImport(file) From 1f6169bd3f4f352397069be4b1613a0a3ada4827 Mon Sep 17 00:00:00 2001 From: Jan W Date: Mon, 7 Oct 2024 14:21:16 +0200 Subject: [PATCH 6/6] refactor: use globalThis --- tests/setup-files/setup.cjs | 2 +- tests/setup-files/setup.js | 2 +- tests/setup-files/setup.mjs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/setup-files/setup.cjs b/tests/setup-files/setup.cjs index 6d506f2..97d2324 100644 --- a/tests/setup-files/setup.cjs +++ b/tests/setup-files/setup.cjs @@ -1,3 +1,3 @@ const path = require('path') -global.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works +globalThis.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works diff --git a/tests/setup-files/setup.js b/tests/setup-files/setup.js index c7234c2..41d601e 100644 --- a/tests/setup-files/setup.js +++ b/tests/setup-files/setup.js @@ -1,3 +1,3 @@ import path from 'path' -global.SETUP_JS_MODULE = path.basename(import.meta.url) +globalThis.SETUP_JS_MODULE = path.basename(import.meta.url) diff --git a/tests/setup-files/setup.mjs b/tests/setup-files/setup.mjs index f4904a3..f72b2a2 100644 --- a/tests/setup-files/setup.mjs +++ b/tests/setup-files/setup.mjs @@ -1,3 +1,3 @@ import path from 'path' -global.SETUP_MJS = path.basename(import.meta.url) +globalThis.SETUP_MJS = path.basename(import.meta.url)