Skip to content

Commit

Permalink
fix(esm): handle parallel imports (#9858)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 22, 2020
1 parent e7ff5b4 commit 32aaff8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
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 || >=13.2.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

0 comments on commit 32aaff8

Please sign in to comment.