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(runner): handle module loaders with default exports #10541

Merged
merged 2 commits into from Sep 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
16 changes: 10 additions & 6 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -106,13 +106,17 @@ async function runTestInternal(
const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
require(testEnvironment),
).default;
const testFramework: TestFramework =
const testFramework: TestFramework = interopRequireDefault(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use import() here with built-in interop, but that creates extra promises 🤷 Also not sure if that works with real ESM

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) =>
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-runtime/src/__mocks__/createRuntime.js
Expand Up @@ -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(
{
Expand Down
Expand Up @@ -7,7 +7,7 @@
*/

import HasteMap from 'jest-haste-map';
const Runtime = require('../');
import Runtime from '../';

jest.mock('jest-haste-map');

Expand Down
6 changes: 2 additions & 4 deletions packages/jest-runtime/src/cli/index.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
});
Expand Down