Skip to content

Commit

Permalink
fix: throw correct error when loading ESM with require (#11260)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 1, 2021
1 parent 4363dd1 commit f10cad1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Expand Up @@ -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: <<REPLACED>>
Ran all test suites matching /native-esm.test.js/i.
Expand Down
11 changes: 11 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Expand Up @@ -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'),
}),
);
});
20 changes: 15 additions & 5 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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: `code` is not defined
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);
Expand Down

0 comments on commit f10cad1

Please sign in to comment.