From 37017ad45715b976ca36ca597a45cd723eb25058 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 1 Apr 2021 20:07:46 +0200 Subject: [PATCH 1/2] fix: throw correct error when loading ESM with `require` --- .../__snapshots__/nativeEsm.test.ts.snap | 2 +- e2e/native-esm/__tests__/native-esm.test.js | 11 ++++++++++ packages/jest-runtime/src/index.ts | 20 ++++++++++++++----- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap index b1e95ff54928..58c9c76d0357 100644 --- a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap +++ b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap @@ -10,7 +10,7 @@ Ran all test suites matching /native-esm.tla.test.js/i. exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = ` Test Suites: 1 passed, 1 total -Tests: 18 passed, 18 total +Tests: 19 passed, 19 total Snapshots: 0 total Time: <> Ran all test suites matching /native-esm.test.js/i. diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index cbc41a136085..72cc6c67aa6a 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -166,3 +166,14 @@ test('handle circular dependency', async () => { expect(moduleA.moduleB.id).toBe('circularDependentB'); expect(moduleA.moduleB.moduleA).toBe(moduleA); }); + +test('require of ESM should throw correct error', () => { + const require = createRequire(import.meta.url); + + expect(() => require('../fromCjs.mjs')).toThrow( + expect.objectContaining({ + code: 'ERR_REQUIRE_ESM', + message: expect.stringContaining('Must use import to load ES Module'), + }), + ); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 04521637efc0..547a3fd4dc63 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -648,16 +648,26 @@ export default class Runtime { modulePath = this._resolveModule(from, moduleName); } + if (this.unstable_shouldLoadAsEsm(modulePath)) { + // Node includes more info in the message + const error = new Error( + `Must use import to load ES Module: ${modulePath}`, + ); + + // @ts-expect-error + error.code = 'ERR_REQUIRE_ESM'; + + throw error; + } + let moduleRegistry; if (options?.isInternalModule) { moduleRegistry = this._internalModuleRegistry; + } else if (this._isolatedModuleRegistry) { + moduleRegistry = this._isolatedModuleRegistry; } else { - if (this._isolatedModuleRegistry) { - moduleRegistry = this._isolatedModuleRegistry; - } else { - moduleRegistry = this._moduleRegistry; - } + moduleRegistry = this._moduleRegistry; } const module = moduleRegistry.get(modulePath); From 0898ca0ceb3e641e930b05c5fa9976538009df61 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 1 Apr 2021 20:10:33 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + packages/jest-runtime/src/index.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1553878924bc..7421a7bd0cd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ - `[jest-runtime]` Fix stack overflow and promise deadlock when importing mutual dependant ES module ([#10892](https://github.com/facebook/jest/pull/10892)) - `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963)) - `[jest-runtime]` Refactor to prevent race condition when linking and evaluating ES Modules ([#11150](https://github.com/facebook/jest/pull/11150)) +- `[jest-runtime]` Throw correct error when attempting to load ESM via `require` ([#11260](https://github.com/facebook/jest/pull/11260)) - `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749)) - `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753)) - `[jest-transform]` [**BREAKING**] Refactor API of transformers to pass an options bag rather than separate `config` and other options ([#10834](https://github.com/facebook/jest/pull/10834)) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 547a3fd4dc63..a903cd366db6 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -654,7 +654,7 @@ export default class Runtime { `Must use import to load ES Module: ${modulePath}`, ); - // @ts-expect-error + // @ts-expect-error: `code` is not defined error.code = 'ERR_REQUIRE_ESM'; throw error;