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: vary ESM cache by query #9914

Merged
merged 3 commits into from
Apr 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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