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): handle parallel imports #9858

Merged
merged 1 commit into from Apr 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
### Fixes

- `[jest-runtime]` Support importing CJS from ESM using `import` statements ([#9850](https://github.com/facebook/jest/pull/9850))
- `[jest-runtime]` Support importing parallel dynamic `import`s ([#9858](https://github.com/facebook/jest/pull/9858))

### Chore & Maintenance

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

exports[`on node >=12.16.0 runs test with native ESM 1`] = `
Test Suites: 1 passed, 1 total
Tests: 10 passed, 10 total
Tests: 11 passed, 11 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
Expand Down
10 changes: 10 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Expand Up @@ -95,3 +95,13 @@ test('handle unlinked dynamic imports', async () => {
test('can import `jest` object', () => {
expect(jestObject).toBeDefined();
});

test('handle dynamic imports of the same module in parallel', async () => {
const [{double: first}, {double: second}] = await Promise.all([
import('../anotherDynamicImport.js'),
import('../anotherDynamicImport.js'),
]);

expect(first).toBe(second);
expect(first(2)).toBe(4);
});
8 changes: 8 additions & 0 deletions e2e/native-esm/anotherDynamicImport.js
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export * from './index';
17 changes: 11 additions & 6 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -139,7 +139,7 @@ class Runtime {
private _moduleMocker: typeof jestMock;
private _isolatedModuleRegistry: ModuleRegistry | null;
private _moduleRegistry: ModuleRegistry;
private _esmoduleRegistry: Map<string, VMModule>;
private _esmoduleRegistry: Map<string, Promise<VMModule>>;
private _needsCoverageMapped: Set<string>;
private _resolver: Resolver;
private _shouldAutoMock: boolean;
Expand Down Expand Up @@ -358,11 +358,16 @@ class Runtime {
},
});

this._esmoduleRegistry.set(cacheKey, module);

await module.link(this.linkModules.bind(this));

await module.evaluate();
this._esmoduleRegistry.set(
cacheKey,
// we wanna put the linking promise in the cache so modules loaded in
// parallel can all await it. We then await it synchronously below, so
// we shouldn't get any unhandled rejections
module
.link(this.linkModules.bind(this))
.then(() => module.evaluate())
.then(() => module),
);
}

const module = this._esmoduleRegistry.get(cacheKey);
Expand Down