Skip to content

Commit 5a9870b

Browse files
authored
fix: correctly handle CJS re-exports in ESM modules (#13856)
1 parent 8313d00 commit 5a9870b

File tree

9 files changed

+65
-3
lines changed

9 files changed

+65
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848))
1010
- `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847))
11+
- `[jest-runtime]` Handle CJS re-exports of node core modules from ESM ([#13853](https://github.com/facebook/jest/pull/13853))
1112
- `[jest-transform]` Downgrade `write-file-atomic` to v4 ([#13853](https://github.com/facebook/jest/pull/13853))
1213
- `[jest-worker]` Ignore IPC messages not intended for Jest ([#13543](https://github.com/facebook/jest/pull/13543))
1314

e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ Time: <<REPLACED>>
4040
Ran all test suites matching /native-esm.test.js/i."
4141
`;
4242
43+
exports[`support re-exports from CJS of core module 1`] = `
44+
"Test Suites: 1 passed, 1 total
45+
Tests: 1 passed, 1 total
46+
Snapshots: 0 total
47+
Time: <<REPLACED>>
48+
Ran all test suites matching /native-esm-core-cjs-reexport.test.js/i."
49+
`;
50+
4351
exports[`supports top-level await 1`] = `
4452
"Test Suites: 1 passed, 1 total
4553
Tests: 1 passed, 1 total

e2e/__tests__/nativeEsm.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ onNodeVersions('>=16.9.0', () => {
6868
});
6969
});
7070

71+
test('support re-exports from CJS of core module', () => {
72+
const {exitCode, stderr, stdout} = runJest(
73+
DIR,
74+
['native-esm-core-cjs-reexport.test.js'],
75+
{nodeOptions: '--experimental-vm-modules --no-warnings'},
76+
);
77+
78+
const {summary} = extractSummary(stderr);
79+
80+
expect(summary).toMatchSnapshot();
81+
expect(stdout).toBe('');
82+
expect(exitCode).toBe(0);
83+
});
84+
7185
test('runs WebAssembly (Wasm) test with native ESM', () => {
7286
const {exitCode, stderr, stdout} = runJest(DIR, ['native-esm-wasm.test.js'], {
7387
nodeOptions: '--experimental-vm-modules --no-warnings',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 {Constants} from '../coreReexport.js';
9+
10+
test('can reexport core CJS requires', () => {
11+
expect(Constants).toHaveProperty('TLS1_VERSION');
12+
});

e2e/native-esm/__tests__/native-esm.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ test('should support importing node core modules', () => {
4646
expect(JSON.parse(readFileSync(packageJsonPath, 'utf8'))).toEqual({
4747
devDependencies: {
4848
'discord.js': '14.3.0',
49+
'iso-constants': '^0.1.2',
4950
yargs: '^17.5.1',
5051
},
5152
jest: {

e2e/native-esm/coreReexport.js

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+
import isoConstants from 'iso-constants';
9+
10+
export const Constants = isoConstants;

e2e/native-esm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"type": "module",
33
"devDependencies": {
44
"discord.js": "14.3.0",
5+
"iso-constants": "^0.1.2",
56
"yargs": "^17.5.1"
67
},
78
"jest": {

e2e/native-esm/yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ __metadata:
234234
languageName: node
235235
linkType: hard
236236

237+
"iso-constants@npm:^0.1.2":
238+
version: 0.1.2
239+
resolution: "iso-constants@npm:0.1.2"
240+
checksum: 5e59939d03b4603d9426704c4dea8c41a4db14a91e34273a1b129aa84e265d6a63456d3543e3be85f47cdf350be5359bd31c59566e5084bdaee618dc65843804
241+
languageName: node
242+
linkType: hard
243+
237244
"lodash.snakecase@npm:^4.1.1":
238245
version: 4.1.1
239246
resolution: "lodash.snakecase@npm:4.1.1"
@@ -287,6 +294,7 @@ __metadata:
287294
resolution: "root-workspace-0b6124@workspace:."
288295
dependencies:
289296
discord.js: 14.3.0
297+
iso-constants: ^0.1.2
290298
yargs: ^17.5.1
291299
languageName: unknown
292300
linkType: soft

packages/jest-runtime/src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,18 @@ export default class Runtime {
951951
const namedExports = new Set(exports);
952952

953953
reexports.forEach(reexport => {
954-
const resolved = this._resolveCjsModule(modulePath, reexport);
954+
if (this._resolver.isCoreModule(reexport)) {
955+
const exports = this.requireModule(modulePath, reexport);
956+
if (exports !== null && typeof exports === 'object') {
957+
Object.keys(exports).forEach(namedExports.add, namedExports);
958+
}
959+
} else {
960+
const resolved = this._resolveCjsModule(modulePath, reexport);
955961

956-
const exports = this.getExportsOfCjs(resolved);
962+
const exports = this.getExportsOfCjs(resolved);
957963

958-
exports.forEach(namedExports.add, namedExports);
964+
exports.forEach(namedExports.add, namedExports);
965+
}
959966
});
960967

961968
this._cjsNamedExports.set(modulePath, namedExports);

0 commit comments

Comments
 (0)