From fdca483fdcd61bc4fb6e7399be63cc077b15def1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 29 Apr 2020 11:23:31 +0200 Subject: [PATCH] fix: vary ESM cache by query (#9914) --- CHANGELOG.md | 1 + .../__snapshots__/nativeEsm.test.ts.snap | 2 +- e2e/native-esm/__tests__/native-esm.test.js | 18 ++++++++++++++++++ packages/jest-runtime/src/index.ts | 15 ++++++++------- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f7b0d20d544..67ef0f399423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap index c16d98c7fafb..953aa9a25bfa 100644 --- a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap +++ b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap @@ -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: <> Ran all test suites. diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 737d422d9c1c..d5a574cb6534 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -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', () => { @@ -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); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 69e1c22de49e..609abc543ad2 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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( @@ -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(