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: correctly handle CJS re-exports in ESM modules #13856

Merged
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 @@ -8,6 +8,7 @@

- `[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))
- `[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))
- `[jest-runtime]` Handle CJS re-exports of node core modules from ESM ([#13853](https://github.com/facebook/jest/pull/13853))
- `[jest-transform]` Downgrade `write-file-atomic` to v4 ([#13853](https://github.com/facebook/jest/pull/13853))
- `[jest-worker]` Ignore IPC messages not intended for Jest ([#13543](https://github.com/facebook/jest/pull/13543))

Expand Down
8 changes: 8 additions & 0 deletions e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Expand Up @@ -40,6 +40,14 @@ Time: <<REPLACED>>
Ran all test suites matching /native-esm.test.js/i."
`;

exports[`support re-exports from CJS of core module 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites matching /native-esm-core-cjs-reexport.test.js/i."
`;

exports[`supports top-level await 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Expand Down
14 changes: 14 additions & 0 deletions e2e/__tests__/nativeEsm.test.ts
Expand Up @@ -68,6 +68,20 @@ onNodeVersions('>=16.9.0', () => {
});
});

test('support re-exports from CJS of core module', () => {
const {exitCode, stderr, stdout} = runJest(
DIR,
['native-esm-core-cjs-reexport.test.js'],
{nodeOptions: '--experimental-vm-modules --no-warnings'},
);

const {summary} = extractSummary(stderr);

expect(summary).toMatchSnapshot();
expect(stdout).toBe('');
expect(exitCode).toBe(0);
});

test('runs WebAssembly (Wasm) test with native ESM', () => {
const {exitCode, stderr, stdout} = runJest(DIR, ['native-esm-wasm.test.js'], {
nodeOptions: '--experimental-vm-modules --no-warnings',
Expand Down
12 changes: 12 additions & 0 deletions e2e/native-esm/__tests__/native-esm-core-cjs-reexport.test.js
@@ -0,0 +1,12 @@
/**
* 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 {Constants} from '../coreReexport.js';

test('can reexport core CJS requires', () => {
expect(Constants).toHaveProperty('TLS1_VERSION');
});
1 change: 1 addition & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Expand Up @@ -46,6 +46,7 @@ test('should support importing node core modules', () => {
expect(JSON.parse(readFileSync(packageJsonPath, 'utf8'))).toEqual({
devDependencies: {
'discord.js': '14.3.0',
'iso-constants': '^0.1.2',
yargs: '^17.5.1',
},
jest: {
Expand Down
10 changes: 10 additions & 0 deletions e2e/native-esm/coreReexport.js
@@ -0,0 +1,10 @@
/**
* 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 isoConstants from 'iso-constants';

export const Constants = isoConstants;
1 change: 1 addition & 0 deletions e2e/native-esm/package.json
Expand Up @@ -2,6 +2,7 @@
"type": "module",
"devDependencies": {
"discord.js": "14.3.0",
"iso-constants": "^0.1.2",
"yargs": "^17.5.1"
},
"jest": {
Expand Down
8 changes: 8 additions & 0 deletions e2e/native-esm/yarn.lock
Expand Up @@ -234,6 +234,13 @@ __metadata:
languageName: node
linkType: hard

"iso-constants@npm:^0.1.2":
version: 0.1.2
resolution: "iso-constants@npm:0.1.2"
checksum: 5e59939d03b4603d9426704c4dea8c41a4db14a91e34273a1b129aa84e265d6a63456d3543e3be85f47cdf350be5359bd31c59566e5084bdaee618dc65843804
languageName: node
linkType: hard

"lodash.snakecase@npm:^4.1.1":
version: 4.1.1
resolution: "lodash.snakecase@npm:4.1.1"
Expand Down Expand Up @@ -287,6 +294,7 @@ __metadata:
resolution: "root-workspace-0b6124@workspace:."
dependencies:
discord.js: 14.3.0
iso-constants: ^0.1.2
yargs: ^17.5.1
languageName: unknown
linkType: soft
Expand Down
13 changes: 10 additions & 3 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -951,11 +951,18 @@ export default class Runtime {
const namedExports = new Set(exports);

reexports.forEach(reexport => {
const resolved = this._resolveCjsModule(modulePath, reexport);
if (this._resolver.isCoreModule(reexport)) {
const exports = this.requireModule(modulePath, reexport);
if (exports !== null && typeof exports === 'object') {
Object.keys(exports).forEach(namedExports.add, namedExports);
}
} else {
const resolved = this._resolveCjsModule(modulePath, reexport);

const exports = this.getExportsOfCjs(resolved);
const exports = this.getExportsOfCjs(resolved);

exports.forEach(namedExports.add, namedExports);
exports.forEach(namedExports.add, namedExports);
}
});

this._cjsNamedExports.set(modulePath, namedExports);
Expand Down