From 0b713d3048e765a3a477095c1fff6de3792fc618 Mon Sep 17 00:00:00 2001 From: Jan W Date: Thu, 3 Oct 2024 17:43:02 +0200 Subject: [PATCH] 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/',