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

feature: allow ESM for test environment #11033

Merged
merged 15 commits into from Apr 30, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@
- `[jest-transform]` Support transpiled transformers ([#11193](https://github.com/facebook/jest/pull/11193))
- `[jest-worker]` Add support for custom task queues and adds a `PriorityQueue` implementation. ([#10921](https://github.com/facebook/jest/pull/10921))
- `[jest-worker]` Add in-order scheduling policy to jest worker ([10902](https://github.com/facebook/jest/pull/10902))
- `[jest-runner]` Possibility to use ESM for test environment ([11033](https://github.com/facebook/jest/pull/11033))
gilles-yvetot marked this conversation as resolved.
Show resolved Hide resolved

### Fixes

Expand Down
16 changes: 16 additions & 0 deletions e2e/test-environment-esm/EnvESM.js
@@ -0,0 +1,16 @@
/**
gilles-yvetot marked this conversation as resolved.
Show resolved Hide resolved
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import NodeEnvironment from 'jest-environment-node';

export default class Env extends NodeEnvironment {
constructor() {
super();
this.global = global;
gilles-yvetot marked this conversation as resolved.
Show resolved Hide resolved
SimenB marked this conversation as resolved.
Show resolved Hide resolved
this.moduleMocker = {};
SimenB marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions e2e/test-environment-esm/__tests__/testUsingESMTestEnv.test.js
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('dummy', () => {
expect(1).toBe(1);
});
5 changes: 5 additions & 0 deletions e2e/test-environment-esm/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "<rootDir>/EnvESM.js"
}
}
38 changes: 34 additions & 4 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -6,6 +6,7 @@
*
*/

import {pathToFileURL} from 'url';
import chalk = require('chalk');
import * as fs from 'graceful-fs';
import sourcemapSupport = require('source-map-support');
Expand Down Expand Up @@ -105,10 +106,39 @@ async function runTestInternal(

const cacheFS = new Map([[path, testSource]]);
const transformer = await createScriptTransformer(config, cacheFS);

const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
transformer.requireAndTranspileModule(testEnvironment),
).default;
let TestEnvironment: typeof JestEnvironment;
try {
TestEnvironment = interopRequireDefault(
transformer.requireAndTranspileModule(testEnvironment),
).default;
} catch (err) {
if (err.code === 'ERR_REQUIRE_ESM') {
try {
const configUrl = pathToFileURL(testEnvironment);

// node `import()` supports URL, but TypeScript doesn't know that
const importedConfig = await import(configUrl.href);

if (!importedConfig.default) {
throw new Error(
`Jest: Failed to load mjs config file ${testEnvironment} - did you use a default export?`,
);
}

TestEnvironment = importedConfig.default;
} catch (innerError) {
if (innerError.message === 'Not supported') {
throw new Error(
`Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${testEnvironment}`,
);
}

throw innerError;
}
} else {
throw err;
}
}
const testFramework: TestFramework = interopRequireDefault(
transformer.requireAndTranspileModule(
process.env.JEST_JASMINE === '1' ? 'jest-jasmine2' : config.testRunner,
Expand Down