Skip to content

Commit

Permalink
fix: vary ESM cache by query (#9914)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Apr 29, 2020
1 parent a5d0b08 commit fdca483
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

### 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 || >=13.2.0 runs test with native ESM 1`] = `
Test Suites: 1 passed, 1 total
Tests: 11 passed, 11 total
Tests: 12 passed, 12 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
Expand Down
18 changes: 18 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {fileURLToPath} from 'url';
import {jest as jestObject} from '@jest/globals';
import staticImportedStateful from '../stateful.mjs';
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
// https://github.com/benmosher/eslint-plugin-import/issues/1739
/* eslint-disable import/no-unresolved */
import staticImportedStatefulWithQuery from '../stateful.mjs?query=1';
import staticImportedStatefulWithAnotherQuery from '../stateful.mjs?query=2';
/* eslint-enable */
import {double} from '../index';

test('should have correct import.meta', () => {
Expand Down Expand Up @@ -107,3 +112,16 @@ test('handle dynamic imports of the same module in parallel', async () => {
expect(first).toBe(second);
expect(first(2)).toBe(4);
});

test('varies module cache by query', () => {
expect(staticImportedStatefulWithQuery).not.toBe(
staticImportedStatefulWithAnotherQuery,
);

expect(staticImportedStatefulWithQuery()).toBe(1);
expect(staticImportedStatefulWithQuery()).toBe(2);
expect(staticImportedStatefulWithAnotherQuery()).toBe(1);
expect(staticImportedStatefulWithQuery()).toBe(3);
expect(staticImportedStatefulWithAnotherQuery()).toBe(2);
expect(staticImportedStatefulWithAnotherQuery()).toBe(3);
});
15 changes: 8 additions & 7 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,15 @@ class Runtime {
return globals;
}

const resolved = this._resolveModule(
referencingModule.identifier,
specifier,
);
const [path, query] = specifier.split('?');

const resolved = this._resolveModule(referencingModule.identifier, path);

if (
this._resolver.isCoreModule(resolved) ||
this.unstable_shouldLoadAsEsm(resolved)
) {
return this.loadEsmModule(resolved);
return this.loadEsmModule(resolved, query);
}

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

const modulePath = this._resolveModule(from, moduleName);
const [path, query] = (moduleName ?? '').split('?');

const modulePath = this._resolveModule(from, path);

return this.loadEsmModule(modulePath);
return this.loadEsmModule(modulePath, query);
}

private async loadCjsAsEsm(
Expand Down

0 comments on commit fdca483

Please sign in to comment.