Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest): support ESM setup files #12

Merged
merged 6 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const config = {
testMatch: [`**/?(*.)+(spec|test).?([cm])[jt]s?(x)`], // From bin/index.js defaults
setupFiles: [
'<rootDir>/tests/setup-files/setup.cjs',
'<rootDir>/tests/setup-files/setup.mjs',
'<rootDir>/tests/setup-files/setup.js',
],
}

export default config
15 changes: 7 additions & 8 deletions src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,28 @@ 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 = (name) => {
dynamicImport = async (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 = async () => 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)
await setup(dynamicImport, 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
/*
Expand Down
4 changes: 2 additions & 2 deletions src/jest.environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<!DOCTYPE html>', {
url: 'http://localhost/',
Expand Down
11 changes: 11 additions & 0 deletions tests/setup-files/index.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
3 changes: 3 additions & 0 deletions tests/setup-files/setup.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const path = require('path')

globalThis.SETUP_CJS = path.basename(__filename) // using some cjs stuff here to make sure it works
3 changes: 3 additions & 0 deletions tests/setup-files/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path'

globalThis.SETUP_JS_MODULE = path.basename(import.meta.url)
3 changes: 3 additions & 0 deletions tests/setup-files/setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path'

globalThis.SETUP_MJS = path.basename(import.meta.url)