From f798a65132e2733371f80bce6d4aaef39b03b08b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 30 Oct 2020 23:36:59 +0100 Subject: [PATCH] chore: handle file URLs in dynamic imports (#10744) --- CHANGELOG.md | 3 ++- e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap | 2 +- e2e/native-esm/__tests__/native-esm.test.js | 6 ++++++ packages/jest-runtime/src/index.ts | 4 ++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a900a6999761..f30c724de98c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,9 @@ - `[babel-plugin-jest-hoist]` Preserve order of hoisted mock nodes within containing block ([#10536](https://github.com/facebook/jest/pull/10536)) - `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711)) - `[jest-circus, jest-jasmine2]` fix: don't assume `stack` is always a string ([#10697](https://github.com/facebook/jest/pull/10697)) -- `[jest-config]` Fix bug introduced in watch mode by PR[#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692)) +- `[jest-config]` Fix bug introduced in watch mode by PR [#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692)) - `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713)) +- `[jest-runtime]` Handle file URLs in dynamic imports ([#10744](https://github.com/facebook/jest/pull/10744)) ### Chore & Maintenance diff --git a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap index a38414134da2..e5566df1fbbd 100644 --- a/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap +++ b/e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap @@ -10,7 +10,7 @@ Ran all test suites matching /native-esm.tla.test.js/i. exports[`on node ^12.16.0 || >=13.7.0 runs test with native ESM 1`] = ` Test Suites: 1 passed, 1 total -Tests: 15 passed, 15 total +Tests: 16 passed, 16 total Snapshots: 0 total Time: <> Ran all test suites matching /native-esm.test.js/i. diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 6ab61f2a21bf..fb2db4d6d7d8 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -145,3 +145,9 @@ test('supports named imports from CJS', () => { expect(Object.keys(defaultFromCjs)).toEqual(['namedFunction', 'default']); }); + +test('supports file urls as imports', async () => { + const dynamic = await import(new URL('../stateful.mjs', import.meta.url)); + + expect(dynamic.default).toBe(staticImportedStateful); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 351f5dc3e9c6..132ffc4a5e00 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -438,6 +438,10 @@ class Runtime { return globals; } + if (specifier.startsWith('file://')) { + specifier = fileURLToPath(specifier); + } + const [path, query] = specifier.split('?'); const resolved = this._resolveModule(referencingIdentifier, path);