From 27227c2bcbce5ad726f058d6099e1a2c8056c8f3 Mon Sep 17 00:00:00 2001 From: Nikita S <259122746+exo-nikita@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:36:03 +0000 Subject: [PATCH 1/2] fix: jest globalSetup/globalTeardown without a preset --- package.json | 3 ++- src/jest.config.js | 4 +++- tests/jest/global-setup/global-setup.cjs | 4 ++++ tests/jest/global-setup/index.global-setup-test.js | 4 ++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/jest/global-setup/global-setup.cjs create mode 100644 tests/jest/global-setup/index.global-setup-test.js diff --git a/package.json b/package.json index 734ef1fd..5a02a2e1 100644 --- a/package.json +++ b/package.json @@ -129,7 +129,7 @@ "scripts": { "test:_bundle": "EXODUS_TEST_IGNORE='tests/{{vendor/jest-extended,inband}/**,vendor/jest-when/when.test.*,jest/jest.resetModules.*,jest/mock/jest.mock.mocks-dir.test.js}' npm run test --", "test": "npm run test:jest --", - "test:all": "npm run test:simple && npm run test:jest && npm run test:tape && npm run test:native && npm run test:esbuild && npm run test:pure && npm run test:fetch && npm run test:jsdom && npm run test:bundle", + "test:all": "npm run test:simple && npm run test:jest && npm run test:tape && npm run test:native && npm run test:esbuild && npm run test:pure && npm run test:fetch && npm run test:jsdom && npm run test:global-setup && npm run test:bundle", "test:native": "EXODUS_TEST_IGNORE='**/jest.transformed/**/*' ./bin/index.js --jest 'tests/**/*.test.{js,cjs,mjs}'", "test:typescript": "node ./bin/index.js --jest --typescript tests/typescript.test.ts", "test:jest": "node ./bin/index.js --jest --esbuild=ts,sum.test.js", @@ -172,6 +172,7 @@ "test:jerryscript": "EXODUS_TEST_ENGINE=jerryscript:bundle npm run test:_bundle -- --bundle-entropy-size 128", "test:fetch": "node ./bin/index.js --jest --drop-network --engine node:pure 'tests/replay/*.test.js'", "test:jsdom": "EXODUS_TEST_JEST_CONFIG='{\"testMatch\":[\"**/*.jsdom-test.js\"],\"testEnvironment\":\"jsdom\", \"rootDir\": \".\"}' ./bin/index.js --jest", + "test:global-setup": "EXODUS_TEST_JEST_CONFIG='{\"testMatch\":[\"**/*.global-setup-test.js\"],\"globalSetup\":\"./tests/jest/global-setup/global-setup.cjs\", \"rootDir\": \".\"}' ./bin/index.js --jest", "coverage": "node ./bin/index.js --jest --esbuild --coverage", "typedoc": "typedoc && mkdir -p doc/assets && cp -r theme/styles doc/assets/", "playwright": "node ./bin/index.js --playwright", diff --git a/src/jest.config.js b/src/jest.config.js index 053de938..c95d8e1a 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -138,7 +138,9 @@ export async function loadJestConfig(...args) { const cleanFile = (file) => file.replace(/^\//g, './') // require is already relative to rootDir if (needPreset(rawConfig) || rawConfig?.globalSetup || rawConfig?.globalTeardown) { - rawConfig.preset = cleanFile(rawConfig.preset) // relative to root dir only at top level, presets shouldn't use + // relative to root dir only at top level, presets shouldn't use + // preset is optional here, we could have entered this branch just because of globalSetup/Teardown + if (rawConfig.preset) rawConfig.preset = cleanFile(rawConfig.preset) if (process.env.EXODUS_TEST_ENVIRONMENT === 'bundle') { throw new Error('jest preset and globalSetup/Teardown not yet supported in bundles') } else { diff --git a/tests/jest/global-setup/global-setup.cjs b/tests/jest/global-setup/global-setup.cjs new file mode 100644 index 00000000..05d3e323 --- /dev/null +++ b/tests/jest/global-setup/global-setup.cjs @@ -0,0 +1,4 @@ +// Used by `npm run test:global-setup`, intentionally without a jest preset +module.exports = async () => { + process.env.EXODUS_TEST_GLOBAL_SETUP_RAN = '1' +} diff --git a/tests/jest/global-setup/index.global-setup-test.js b/tests/jest/global-setup/index.global-setup-test.js new file mode 100644 index 00000000..3693452f --- /dev/null +++ b/tests/jest/global-setup/index.global-setup-test.js @@ -0,0 +1,4 @@ +// Run with `npm run test:global-setup` +test('jest globalSetup without a preset ran before tests', () => { + expect(process.env.EXODUS_TEST_GLOBAL_SETUP_RAN).toBe('1') +}) From b6a1345edd99e8fbcf6e3d743fb221b525d69169 Mon Sep 17 00:00:00 2001 From: Nikita S <259122746+exo-nikita@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:39:44 +0000 Subject: [PATCH 2/2] feat: support in jest globalSetup/globalTeardown --- package.json | 2 +- src/jest.config.js | 7 +++++-- tests/jest/global-setup/global-teardown.cjs | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 tests/jest/global-setup/global-teardown.cjs diff --git a/package.json b/package.json index 5a02a2e1..2bf14500 100644 --- a/package.json +++ b/package.json @@ -172,7 +172,7 @@ "test:jerryscript": "EXODUS_TEST_ENGINE=jerryscript:bundle npm run test:_bundle -- --bundle-entropy-size 128", "test:fetch": "node ./bin/index.js --jest --drop-network --engine node:pure 'tests/replay/*.test.js'", "test:jsdom": "EXODUS_TEST_JEST_CONFIG='{\"testMatch\":[\"**/*.jsdom-test.js\"],\"testEnvironment\":\"jsdom\", \"rootDir\": \".\"}' ./bin/index.js --jest", - "test:global-setup": "EXODUS_TEST_JEST_CONFIG='{\"testMatch\":[\"**/*.global-setup-test.js\"],\"globalSetup\":\"./tests/jest/global-setup/global-setup.cjs\", \"rootDir\": \".\"}' ./bin/index.js --jest", + "test:global-setup": "EXODUS_TEST_JEST_CONFIG='{\"testMatch\":[\"**/*.global-setup-test.js\"],\"globalSetup\":\"/tests/jest/global-setup/global-setup.cjs\",\"globalTeardown\":\"./tests/jest/global-setup/global-teardown.cjs\", \"rootDir\": \".\"}' ./bin/index.js --jest", "coverage": "node ./bin/index.js --jest --esbuild --coverage", "typedoc": "typedoc && mkdir -p doc/assets && cp -r theme/styles doc/assets/", "playwright": "node ./bin/index.js --playwright", diff --git a/src/jest.config.js b/src/jest.config.js index c95d8e1a..5609db31 100644 --- a/src/jest.config.js +++ b/src/jest.config.js @@ -139,8 +139,11 @@ export async function loadJestConfig(...args) { const cleanFile = (file) => file.replace(/^\//g, './') // require is already relative to rootDir if (needPreset(rawConfig) || rawConfig?.globalSetup || rawConfig?.globalTeardown) { // relative to root dir only at top level, presets shouldn't use - // preset is optional here, we could have entered this branch just because of globalSetup/Teardown - if (rawConfig.preset) rawConfig.preset = cleanFile(rawConfig.preset) + // each is optional here, we could have entered this branch just because of one of them + for (const key of ['preset', 'globalSetup', 'globalTeardown']) { + if (rawConfig[key]) rawConfig[key] = cleanFile(rawConfig[key]) + } + if (process.env.EXODUS_TEST_ENVIRONMENT === 'bundle') { throw new Error('jest preset and globalSetup/Teardown not yet supported in bundles') } else { diff --git a/tests/jest/global-setup/global-teardown.cjs b/tests/jest/global-setup/global-teardown.cjs new file mode 100644 index 00000000..09e29dc9 --- /dev/null +++ b/tests/jest/global-setup/global-teardown.cjs @@ -0,0 +1,4 @@ +// Used by `npm run test:global-setup`, intentionally without a jest preset +module.exports = async () => { + if (process.env.EXODUS_TEST_GLOBAL_SETUP_RAN !== '1') throw new Error('globalSetup did not run') +}