Skip to content

Commit

Permalink
refactor: kill require
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 committed Oct 3, 2024
1 parent 6ce4c3a commit 0b713d3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,30 +101,25 @@ 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')
}

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 || []) await dynamicImport(file)
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

0 comments on commit 0b713d3

Please sign in to comment.