diff --git a/CHANGELOG.md b/CHANGELOG.md index dbe5213906d0..f4c3d7053925 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap index e98ffe6d8f5b..1e8b6bd6ed39 100644 --- a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap +++ b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap @@ -40,6 +40,14 @@ Time: <> 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: <> +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 diff --git a/e2e/__tests__/nativeEsm.test.ts b/e2e/__tests__/nativeEsm.test.ts index 47ee045d0a3e..be7e41600409 100644 --- a/e2e/__tests__/nativeEsm.test.ts +++ b/e2e/__tests__/nativeEsm.test.ts @@ -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', diff --git a/e2e/native-esm/__tests__/native-esm-core-cjs-reexport.test.js b/e2e/native-esm/__tests__/native-esm-core-cjs-reexport.test.js new file mode 100644 index 000000000000..a64a487c0804 --- /dev/null +++ b/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'); +}); diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 4b253fe6bca1..c452fda7d313 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -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: { diff --git a/e2e/native-esm/coreReexport.js b/e2e/native-esm/coreReexport.js new file mode 100644 index 000000000000..173727f9a442 --- /dev/null +++ b/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; diff --git a/e2e/native-esm/package.json b/e2e/native-esm/package.json index 60993c0776ca..7ef4ed423ae8 100644 --- a/e2e/native-esm/package.json +++ b/e2e/native-esm/package.json @@ -2,6 +2,7 @@ "type": "module", "devDependencies": { "discord.js": "14.3.0", + "iso-constants": "^0.1.2", "yargs": "^17.5.1" }, "jest": { diff --git a/e2e/native-esm/yarn.lock b/e2e/native-esm/yarn.lock index 1cacfd5f1c46..fdea0dbf3934 100644 --- a/e2e/native-esm/yarn.lock +++ b/e2e/native-esm/yarn.lock @@ -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" @@ -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 diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f050d0d01e58..fe3a5fb81ffb 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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);