Skip to content

Commit fdca483

Browse files
authored
fix: vary ESM cache by query (#9914)
1 parent a5d0b08 commit fdca483

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `[jest-haste-map]` Add missing `@types/graceful-fs` dependency ([#9913](https://github.com/facebook/jest/pull/9913))
88
- `[jest-runner]` Correctly serialize `Set` passed to worker ([#9915](https://github.com/facebook/jest/pull/9915))
9+
- `[jest-runtime]` Vary ESM cache by query ([#9914](https://github.com/facebook/jest/pull/9914))
910

1011
### Chore & Maintenance
1112

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.2.0 runs test with native ESM 1`] = `
44
Test Suites: 1 passed, 1 total
5-
Tests: 11 passed, 11 total
5+
Tests: 12 passed, 12 total
66
Snapshots: 0 total
77
Time: <<REPLACED>>
88
Ran all test suites.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import {fileURLToPath} from 'url';
1414
import {jest as jestObject} from '@jest/globals';
1515
import staticImportedStateful from '../stateful.mjs';
1616
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
17+
// https://github.com/benmosher/eslint-plugin-import/issues/1739
18+
/* eslint-disable import/no-unresolved */
19+
import staticImportedStatefulWithQuery from '../stateful.mjs?query=1';
20+
import staticImportedStatefulWithAnotherQuery from '../stateful.mjs?query=2';
21+
/* eslint-enable */
1722
import {double} from '../index';
1823

1924
test('should have correct import.meta', () => {
@@ -107,3 +112,16 @@ test('handle dynamic imports of the same module in parallel', async () => {
107112
expect(first).toBe(second);
108113
expect(first(2)).toBe(4);
109114
});
115+
116+
test('varies module cache by query', () => {
117+
expect(staticImportedStatefulWithQuery).not.toBe(
118+
staticImportedStatefulWithAnotherQuery,
119+
);
120+
121+
expect(staticImportedStatefulWithQuery()).toBe(1);
122+
expect(staticImportedStatefulWithQuery()).toBe(2);
123+
expect(staticImportedStatefulWithAnotherQuery()).toBe(1);
124+
expect(staticImportedStatefulWithQuery()).toBe(3);
125+
expect(staticImportedStatefulWithAnotherQuery()).toBe(2);
126+
expect(staticImportedStatefulWithAnotherQuery()).toBe(3);
127+
});

packages/jest-runtime/src/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,16 +399,15 @@ class Runtime {
399399
return globals;
400400
}
401401

402-
const resolved = this._resolveModule(
403-
referencingModule.identifier,
404-
specifier,
405-
);
402+
const [path, query] = specifier.split('?');
403+
404+
const resolved = this._resolveModule(referencingModule.identifier, path);
406405

407406
if (
408407
this._resolver.isCoreModule(resolved) ||
409408
this.unstable_shouldLoadAsEsm(resolved)
410409
) {
411-
return this.loadEsmModule(resolved);
410+
return this.loadEsmModule(resolved, query);
412411
}
413412

414413
return this.loadCjsAsEsm(
@@ -427,9 +426,11 @@ class Runtime {
427426
'You need to run with a version of node that supports ES Modules in the VM API.',
428427
);
429428

430-
const modulePath = this._resolveModule(from, moduleName);
429+
const [path, query] = (moduleName ?? '').split('?');
430+
431+
const modulePath = this._resolveModule(from, path);
431432

432-
return this.loadEsmModule(modulePath);
433+
return this.loadEsmModule(modulePath, query);
433434
}
434435

435436
private async loadCjsAsEsm(

0 commit comments

Comments
 (0)