From 4b3ed1270a50cd26c3b6f12d052144f1a8e56c3e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 29 Apr 2020 09:47:19 +0200 Subject: [PATCH 1/2] fix: vary ESM cache by query --- CHANGELOG.md | 1 + .../__snapshots__/nativeEsm.test.ts.snap | 2 +- e2e/native-esm/__tests__/native-esm.test.js | 15 +++++++++++++++ packages/jest-runtime/src/index.ts | 15 ++++++++------- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50237a17f7e2..b5585f1e8e30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[jest-haste-map]` Add missing `@types/graceful-fs` dependency ([#9913](https://github.com/facebook/jest/pull/9913)) +- `[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..6ac6cdf54300 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -14,6 +14,8 @@ import {fileURLToPath} from 'url'; import {jest as jestObject} from '@jest/globals'; import staticImportedStateful from '../stateful.mjs'; import staticImportedStatefulFromCjs from '../fromCjs.mjs'; +import staticImportedStatefulWithQuery from '../stateful.mjs?query=1'; +import staticImportedStatefulWithAnotherQuery from '../stateful.mjs?query=2'; import {double} from '../index'; test('should have correct import.meta', () => { @@ -107,3 +109,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( From e79b1ccbb00e03abe1dac21802750a268cae8ccb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 29 Apr 2020 10:10:36 +0200 Subject: [PATCH 2/2] "fix" lint --- e2e/native-esm/__tests__/native-esm.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 6ac6cdf54300..d5a574cb6534 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -14,8 +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', () => {