Skip to content

Commit f10cad1

Browse files
authored
fix: throw correct error when loading ESM with require (#11260)
1 parent 4363dd1 commit f10cad1

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- `[jest-runtime]` Fix stack overflow and promise deadlock when importing mutual dependant ES module ([#10892](https://github.com/facebook/jest/pull/10892))
6464
- `[jest-runtime]` Prevent global module registry from leaking into `isolateModules` registry ([#10963](https://github.com/facebook/jest/pull/10963))
6565
- `[jest-runtime]` Refactor to prevent race condition when linking and evaluating ES Modules ([#11150](https://github.com/facebook/jest/pull/11150))
66+
- `[jest-runtime]` Throw correct error when attempting to load ESM via `require` ([#11260](https://github.com/facebook/jest/pull/11260))
6667
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
6768
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
6869
- `[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))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Ran all test suites matching /native-esm.tla.test.js/i.
1010
1111
exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = `
1212
Test Suites: 1 passed, 1 total
13-
Tests: 18 passed, 18 total
13+
Tests: 19 passed, 19 total
1414
Snapshots: 0 total
1515
Time: <<REPLACED>>
1616
Ran all test suites matching /native-esm.test.js/i.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ test('handle circular dependency', async () => {
166166
expect(moduleA.moduleB.id).toBe('circularDependentB');
167167
expect(moduleA.moduleB.moduleA).toBe(moduleA);
168168
});
169+
170+
test('require of ESM should throw correct error', () => {
171+
const require = createRequire(import.meta.url);
172+
173+
expect(() => require('../fromCjs.mjs')).toThrow(
174+
expect.objectContaining({
175+
code: 'ERR_REQUIRE_ESM',
176+
message: expect.stringContaining('Must use import to load ES Module'),
177+
}),
178+
);
179+
});

packages/jest-runtime/src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,26 @@ export default class Runtime {
648648
modulePath = this._resolveModule(from, moduleName);
649649
}
650650

651+
if (this.unstable_shouldLoadAsEsm(modulePath)) {
652+
// Node includes more info in the message
653+
const error = new Error(
654+
`Must use import to load ES Module: ${modulePath}`,
655+
);
656+
657+
// @ts-expect-error: `code` is not defined
658+
error.code = 'ERR_REQUIRE_ESM';
659+
660+
throw error;
661+
}
662+
651663
let moduleRegistry;
652664

653665
if (options?.isInternalModule) {
654666
moduleRegistry = this._internalModuleRegistry;
667+
} else if (this._isolatedModuleRegistry) {
668+
moduleRegistry = this._isolatedModuleRegistry;
655669
} else {
656-
if (this._isolatedModuleRegistry) {
657-
moduleRegistry = this._isolatedModuleRegistry;
658-
} else {
659-
moduleRegistry = this._moduleRegistry;
660-
}
670+
moduleRegistry = this._moduleRegistry;
661671
}
662672

663673
const module = moduleRegistry.get(modulePath);

0 commit comments

Comments
 (0)