Skip to content

Commit

Permalink
fix(esm): handle parallel imports
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 22, 2020
1 parent fa4cbbf commit ee5ee0a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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
Original file line number Diff line number Diff line change
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: 9 passed, 9 total
Tests: 10 passed, 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ test('handle unlinked dynamic imports', async () => {

expect(deepDouble(4)).toBe(8);
});

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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 @@ -357,11 +357,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 ee5ee0a

Please sign in to comment.