Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esm): evaluate node core modules on dynamic import #10622

Merged
merged 2 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

### Fixes

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

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = `
Test Suites: 1 passed, 1 total
Tests: 13 passed, 13 total
Tests: 14 passed, 14 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
Expand Down
7 changes: 7 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ test('should support importing node core modules', () => {
});
});

test('should support importing node core modules dynamically', async () => {
// it's important that this module has _not_ been imported at the top level
const assert = await import('assert');

expect(typeof assert.strictEqual).toBe('function');
});

test('dynamic import should work', async () => {
const {double: importedDouble} = await import('../index');

Expand Down
34 changes: 17 additions & 17 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ class Runtime {
return this.loadEsmModule(modulePath, query);
}

private async loadCjsAsEsm(
private loadCjsAsEsm(
from: Config.Path,
modulePath: Config.Path,
context: VMContext,
Expand All @@ -483,13 +483,7 @@ class Runtime {
{context, identifier: modulePath},
);

await module.link(() => {
throw new Error('This should never happen');
});

await module.evaluate();

return module;
return evaluateSyntheticModule(module);
}

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

return new SyntheticModule(
const module = new SyntheticModule(
['default', ...Object.keys(required)],
function () {
// @ts-expect-error: TS doesn't know what `this` is
Expand All @@ -1172,6 +1166,8 @@ class Runtime {
// should identifier be `node://${moduleName}`?
{context, identifier: moduleName},
);

return evaluateSyntheticModule(module);
}

private _getMockedNativeModule(): typeof nativeModule.Module {
Expand Down Expand Up @@ -1667,7 +1663,7 @@ class Runtime {
return {...this.getGlobalsFromEnvironment(), jest};
}

private async getGlobalsForEsm(
private getGlobalsForEsm(
from: Config.Path,
context: VMContext,
): Promise<VMModule> {
Expand Down Expand Up @@ -1695,13 +1691,7 @@ class Runtime {
{context, identifier: '@jest/globals'},
);

await module.link(() => {
throw new Error('This should never happen');
});

await module.evaluate();

return module;
return evaluateSyntheticModule(module);
}

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

async function evaluateSyntheticModule(module: SyntheticModule) {
await module.link(() => {
throw new Error('This should never happen');
});

await module.evaluate();

return module;
}

export = Runtime;