Skip to content

Commit 2abd495

Browse files
authored
feat: requireAndTranspileModule support ESM (#11232)
1 parent e1bc733 commit 2abd495

File tree

19 files changed

+247
-130
lines changed

19 files changed

+247
-130
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
- `[jest-core]` Add support for `testSequencer` written in ESM ([#11207](https://github.com/facebook/jest/pull/11207))
1919
- `[jest-core]` Add support for `globalSetup` and `globalTeardown` written in ESM ([#11267](https://github.com/facebook/jest/pull/11267))
2020
- `[jest-core]` Add support for `watchPlugins` written in ESM ([#11315](https://github.com/facebook/jest/pull/11315))
21+
- `[jest-core]` Add support for `runner` written in ESM ([#11232](https://github.com/facebook/jest/pull/11232))
2122
- `[jest-environment-node]` Add AbortController to globals ([#11182](https://github.com/facebook/jest/pull/11182))
2223
- `[@jest/fake-timers]` Update to `@sinonjs/fake-timers` to v7 ([#11198](https://github.com/facebook/jest/pull/11198))
2324
- `[jest-haste-map]` Handle injected scm clocks ([#10966](https://github.com/facebook/jest/pull/10966))
2425
- `[jest-haste-map]` Add `enableSymlinks` configuration option to follow symlinks for test files ([#9351](https://github.com/facebook/jest/pull/9351))
2526
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
27+
- `[jest-repl]` Add support for `testEnvironment` written in ESM ([#11232](https://github.com/facebook/jest/pull/11232))
2628
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
2729
- `[jest-runner]` [**BREAKING**] Run transforms over `runnner` ([#8823](https://github.com/facebook/jest/pull/8823))
2830
- `[jest-runner]` [**BREAKING**] Run transforms over `testRunnner` ([#8823](https://github.com/facebook/jest/pull/8823))
2931
- `[jest-runner]` Possibility to use ESM for test environment ([11033](https://github.com/facebook/jest/pull/11033))
32+
- `[jest-runner]` Add support for `testRunner` written in ESM ([#11232](https://github.com/facebook/jest/pull/11232))
3033
- `[jest-runtime]` Detect reexports from CJS as named exports in ESM ([#10988](https://github.com/facebook/jest/pull/10988))
3134
- `[jest-runtime]` Support for async code transformations ([#11191](https://github.com/facebook/jest/pull/11191) & [#11220](https://github.com/facebook/jest/pull/11220))
3235
- `[jest-reporters]` Add static filepath property to all reporters ([#11015](https://github.com/facebook/jest/pull/11015))
@@ -37,6 +40,10 @@
3740
- `[jest-transform]` [**BREAKING**] Do not export `ScriptTransformer` class, instead export the async function `createScriptTransformer` ([#11163](https://github.com/facebook/jest/pull/11163))
3841
- `[jest-transform]` Async code transformations ([#9889](https://github.com/facebook/jest/pull/9889))
3942
- `[jest-transform]` Support transpiled transformers ([#11193](https://github.com/facebook/jest/pull/11193))
43+
- `[jest-transform]` [**BREAKING**] `requireAndTranspileModule` always return a `Promise`, and the third parameter type is changed to `RequireAndTranspileModuleOptions` which accept `applyInteropRequireDefault` option ([#11232](https://github.com/facebook/jest/pull/11232))
44+
- `[jest-transform]` [**BREAKING**] `createTranspilingRequire` return function which return a `Promise` now ([#11232](https://github.com/facebook/jest/pull/11232))
45+
- `[jest-util]` add requireOrImportModule for importing CJS or ESM ([#11199](https://github.com/facebook/jest/pull/11199))
46+
- `[jest-util]` add `applyInteropRequireDefault` option on `requireOrImportModule` ([#11232](https://github.com/facebook/jest/pull/11232))
4047
- `[jest-watcher]` Added support for clearing the line when `<C-u>` is pressed in a watch mode pattern prompt ([#11358](https://github.com/facebook/jest/pull/11358))
4148
- `[jest-worker]` Add support for custom task queues and adds a `PriorityQueue` implementation. ([#10921](https://github.com/facebook/jest/pull/10921))
4249
- `[jest-worker]` Add in-order scheduling policy to jest worker ([10902](https://github.com/facebook/jest/pull/10902))

e2e/__tests__/globalSetup.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const customTransformDIR = path.join(
2727
const nodeModulesDIR = path.join(tmpdir(), 'jest-global-setup-node-modules');
2828
const rejectionDir = path.join(tmpdir(), 'jest-global-setup-rejection');
2929
const e2eDir = path.resolve(__dirname, '../global-setup');
30+
const esmTmpDir = path.join(tmpdir(), 'jest-global-setup-esm');
3031

3132
beforeAll(() => {
3233
runYarnInstall(e2eDir);
@@ -39,6 +40,7 @@ beforeEach(() => {
3940
cleanup(customTransformDIR);
4041
cleanup(nodeModulesDIR);
4142
cleanup(rejectionDir);
43+
cleanup(esmTmpDir);
4244
});
4345

4446
afterAll(() => {
@@ -48,6 +50,7 @@ afterAll(() => {
4850
cleanup(customTransformDIR);
4951
cleanup(nodeModulesDIR);
5052
cleanup(rejectionDir);
53+
cleanup(esmTmpDir);
5154
});
5255

5356
test('globalSetup is triggered once before all test suites', () => {

e2e/__tests__/globalTeardown.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const DIR = path.join(tmpdir(), 'jest-global-teardown');
1717
const project1DIR = path.join(tmpdir(), 'jest-global-teardown-project-1');
1818
const project2DIR = path.join(tmpdir(), 'jest-global-teardown-project-2');
1919
const e2eDir = path.resolve(__dirname, '../global-teardown');
20+
const esmTmpDir = path.join(tmpdir(), 'jest-global-teardown-esm');
2021

2122
beforeAll(() => {
2223
runYarnInstall(e2eDir);
@@ -26,11 +27,13 @@ beforeEach(() => {
2627
cleanup(DIR);
2728
cleanup(project1DIR);
2829
cleanup(project2DIR);
30+
cleanup(esmTmpDir);
2931
});
3032
afterAll(() => {
3133
cleanup(DIR);
3234
cleanup(project1DIR);
3335
cleanup(project2DIR);
36+
cleanup(esmTmpDir);
3437
});
3538

3639
test('globalTeardown is triggered once after all test suites', () => {

e2e/__tests__/transform.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,33 @@ onNodeVersions('^12.17.0 || >=13.2.0', () => {
315315
expect(json.numPassedTests).toBe(1);
316316
});
317317
});
318+
319+
describe('transform-esm-runner', () => {
320+
const dir = path.resolve(__dirname, '../transform/transform-esm-runner');
321+
test('runs test with native ESM', () => {
322+
const {json, stderr} = runWithJson(dir, ['--no-cache'], {
323+
nodeOptions: '--experimental-vm-modules',
324+
});
325+
326+
expect(stderr).toMatch(/PASS/);
327+
expect(json.success).toBe(true);
328+
expect(json.numPassedTests).toBe(1);
329+
});
330+
});
331+
332+
describe('transform-esm-testrunner', () => {
333+
const dir = path.resolve(
334+
__dirname,
335+
'../transform/transform-esm-testrunner',
336+
);
337+
test('runs test with native ESM', () => {
338+
const {json, stderr} = runWithJson(dir, ['--no-cache'], {
339+
nodeOptions: '--experimental-vm-modules',
340+
});
341+
342+
expect(stderr).toMatch(/PASS/);
343+
expect(json.success).toBe(true);
344+
expect(json.numPassedTests).toBe(1);
345+
});
346+
});
318347
});

e2e/test-environment-async/__tests__/custom.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*
77
*/
88
'use strict';
9-
/* eslint-env browser*/
109

1110
test('setup', () => {
1211
expect(global.setup).toBe('setup');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
it('should add two numbers', () => {
9+
expect(1 + 1).toBe(2);
10+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"rootDir": "./",
5+
"runner": "<rootDir>/runner.mjs"
6+
}
7+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import testResult from '@jest/test-result';
9+
10+
const {createEmptyTestResult} = testResult;
11+
12+
export default class BaseTestRunner {
13+
constructor(globalConfig, context) {
14+
this._globalConfig = globalConfig;
15+
this._context = context || {};
16+
}
17+
18+
async runTests(tests, watcher, onStart, onResult, onFailure) {
19+
return tests.reduce(
20+
(promise, test) =>
21+
promise
22+
.then(async () => {
23+
await onStart(test);
24+
return {
25+
...createEmptyTestResult(),
26+
numPassingTests: 1,
27+
testFilePath: test.path,
28+
testResults: [
29+
{
30+
ancestorTitles: [],
31+
duration: 2,
32+
failureDetails: [],
33+
failureMessages: [],
34+
fullName: 'sample test',
35+
location: null,
36+
numPassingAsserts: 1,
37+
status: 'passed',
38+
title: 'sample test',
39+
},
40+
],
41+
};
42+
})
43+
.then(result => onResult(test, result))
44+
.catch(err => onFailure(test, err)),
45+
Promise.resolve(),
46+
);
47+
}
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
it('should add two numbers', () => {
9+
expect(1 + 1).toBe(2);
10+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"rootDir": "./",
5+
"testRunner": "<rootDir>/test-runner.mjs"
6+
}
7+
}

0 commit comments

Comments
 (0)