Skip to content

Commit e0f8c1f

Browse files
authored
fix(esm): evaluate node core modules on dynamic import (#10622)
1 parent c41420f commit e0f8c1f

4 files changed

Lines changed: 28 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
### Fixes
88

9-
- `[jest-runner, jest-runtime]` fix: `require.main` undefined with `createRequire()` ([#10610](https://github.com/facebook/jest/pull/10610))
9+
- `[jest-runner, jest-runtime]` `require.main` should not be `undefined` with `createRequire()` ([#10610](https://github.com/facebook/jest/pull/10610))
1010
- `[jest-runtime]` add missing `module.path` property ([#10615](https://github.com/facebook/jest/pull/10615))
11-
- `[jest-runtime]` fix: add `mainModule` instance variable to runtime ([#10621](https://github.com/facebook/jest/pull/10621))
11+
- `[jest-runtime]` Add `mainModule` instance variable to runtime ([#10621](https://github.com/facebook/jest/pull/10621))
12+
- `[jest-runtime]` Evaluate Node core modules on dynamic `import()` ([#10622](https://github.com/facebook/jest/pull/10622))
1213
- `[jest-validate]` Show suggestion only when unrecognized cli param is longer than 1 character ([#10604](https://github.com/facebook/jest/pull/10604))
1314
- `[jest-validate]` Validate `testURL` as CLI option ([#10595](https://github.com/facebook/jest/pull/10595))
1415

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = `
44
Test Suites: 1 passed, 1 total
5-
Tests: 13 passed, 13 total
5+
Tests: 14 passed, 14 total
66
Snapshots: 0 total
77
Time: <<REPLACED>>
88
Ran all test suites.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ test('should support importing node core modules', () => {
4949
});
5050
});
5151

52+
test('should support importing node core modules dynamically', async () => {
53+
// it's important that this module has _not_ been imported at the top level
54+
const assert = await import('assert');
55+
56+
expect(typeof assert.strictEqual).toBe('function');
57+
});
58+
5259
test('dynamic import should work', async () => {
5360
const {double: importedDouble} = await import('../index');
5461

packages/jest-runtime/src/index.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class Runtime {
466466
return this.loadEsmModule(modulePath, query);
467467
}
468468

469-
private async loadCjsAsEsm(
469+
private loadCjsAsEsm(
470470
from: Config.Path,
471471
modulePath: Config.Path,
472472
context: VMContext,
@@ -483,13 +483,7 @@ class Runtime {
483483
{context, identifier: modulePath},
484484
);
485485

486-
await module.link(() => {
487-
throw new Error('This should never happen');
488-
});
489-
490-
await module.evaluate();
491-
492-
return module;
486+
return evaluateSyntheticModule(module);
493487
}
494488

495489
requireModule<T = unknown>(
@@ -1159,7 +1153,7 @@ class Runtime {
11591153
private _importCoreModule(moduleName: string, context: VMContext) {
11601154
const required = this._requireCoreModule(moduleName);
11611155

1162-
return new SyntheticModule(
1156+
const module = new SyntheticModule(
11631157
['default', ...Object.keys(required)],
11641158
function () {
11651159
// @ts-expect-error: TS doesn't know what `this` is
@@ -1172,6 +1166,8 @@ class Runtime {
11721166
// should identifier be `node://${moduleName}`?
11731167
{context, identifier: moduleName},
11741168
);
1169+
1170+
return evaluateSyntheticModule(module);
11751171
}
11761172

11771173
private _getMockedNativeModule(): typeof nativeModule.Module {
@@ -1667,7 +1663,7 @@ class Runtime {
16671663
return {...this.getGlobalsFromEnvironment(), jest};
16681664
}
16691665

1670-
private async getGlobalsForEsm(
1666+
private getGlobalsForEsm(
16711667
from: Config.Path,
16721668
context: VMContext,
16731669
): Promise<VMModule> {
@@ -1695,13 +1691,7 @@ class Runtime {
16951691
{context, identifier: '@jest/globals'},
16961692
);
16971693

1698-
await module.link(() => {
1699-
throw new Error('This should never happen');
1700-
});
1701-
1702-
await module.evaluate();
1703-
1704-
return module;
1694+
return evaluateSyntheticModule(module);
17051695
}
17061696

17071697
private getGlobalsFromEnvironment(): JestGlobals {
@@ -1753,4 +1743,14 @@ function notEmpty<T>(value: T | null | undefined): value is T {
17531743
return value !== null && value !== undefined;
17541744
}
17551745

1746+
async function evaluateSyntheticModule(module: SyntheticModule) {
1747+
await module.link(() => {
1748+
throw new Error('This should never happen');
1749+
});
1750+
1751+
await module.evaluate();
1752+
1753+
return module;
1754+
}
1755+
17561756
export = Runtime;

0 commit comments

Comments
 (0)