From 6499debdaa4fde3cc83ad1f42b3210478a389026 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 21 Sep 2020 20:57:36 +0200 Subject: [PATCH] fix(runner): handle module loaders with default exports (#10541) --- CHANGELOG.md | 1 + packages/jest-runner/src/runTest.ts | 16 ++++++++++------ .../jest-runtime/src/__mocks__/createRuntime.js | 6 +++--- .../src/__tests__/Runtime-statics.test.js | 2 +- packages/jest-runtime/src/cli/index.ts | 6 ++---- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97a12828567f..99689b1d4169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[jest-circus, jest-config, jest-runtime]` Add new `injectGlobals` config and CLI option to disable injecting global variables into the runtime ([#10484](https://github.com/facebook/jest/pull/10484)) - `[jest-each]` Fixes `.each` type to always be callable ([#10447](https://github.com/facebook/jest/pull/10447)) +- `[jest-runner]` Add support for `moduleLoader`s with `default` exports ([#10541](https://github.com/facebook/jest/pull/10541)) ### Fixes diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index dedd560cc12c..36d4ebb03cd9 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -106,13 +106,17 @@ async function runTestInternal( const TestEnvironment: typeof JestEnvironment = interopRequireDefault( require(testEnvironment), ).default; - const testFramework: TestFramework = + const testFramework: TestFramework = interopRequireDefault( process.env.JEST_CIRCUS === '1' - ? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies - : require(config.testRunner); - const Runtime: typeof RuntimeClass = config.moduleLoader - ? require(config.moduleLoader) - : require('jest-runtime'); + ? // eslint-disable-next-line import/no-extraneous-dependencies + require('jest-circus/runner') + : require(config.testRunner), + ).default; + const Runtime: typeof RuntimeClass = interopRequireDefault( + config.moduleLoader + ? require(config.moduleLoader) + : require('jest-runtime'), + ).default; const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout; const consoleFormatter = (type: LogType, message: LogMessage) => diff --git a/packages/jest-runtime/src/__mocks__/createRuntime.js b/packages/jest-runtime/src/__mocks__/createRuntime.js index 1512da0cb3eb..5c0864d5369a 100644 --- a/packages/jest-runtime/src/__mocks__/createRuntime.js +++ b/packages/jest-runtime/src/__mocks__/createRuntime.js @@ -8,10 +8,10 @@ import path from 'path'; module.exports = async function createRuntime(filename, config) { - const NodeEnvironment = require('jest-environment-node'); - const Runtime = require('../'); + const {default: NodeEnvironment} = await import('jest-environment-node'); + const {default: Runtime} = await import('../'); - const {normalize} = require('jest-config'); + const {normalize} = await import('jest-config'); config = normalize( { diff --git a/packages/jest-runtime/src/__tests__/Runtime-statics.test.js b/packages/jest-runtime/src/__tests__/Runtime-statics.test.js index a4fc811dbeb1..a198c99051e9 100644 --- a/packages/jest-runtime/src/__tests__/Runtime-statics.test.js +++ b/packages/jest-runtime/src/__tests__/Runtime-statics.test.js @@ -7,7 +7,7 @@ */ import HasteMap from 'jest-haste-map'; -const Runtime = require('../'); +import Runtime from '../'; jest.mock('jest-haste-map'); diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index 490848a2ba1e..2f1a9b1ccd57 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -16,7 +16,6 @@ import {setGlobal, tryRealpath} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; import {deprecationEntries, readConfig} from 'jest-config'; import {VERSION} from '../version'; -import type {Context} from '../types'; import * as args from './args'; export async function run( @@ -67,11 +66,10 @@ export async function run( automock: false, }; - // Break circular dependency - const Runtime: any = require('..'); + const Runtime: typeof import('..') = require('..'); try { - const hasteMap: Context = await Runtime.createContext(config, { + const hasteMap = await Runtime.createContext(config, { maxWorkers: Math.max(cpus().length - 1, 1), watchman: globalConfig.watchman, });