From 77a6df23267940d414df99357ead66e95e2f5650 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 30 Jun 2024 14:33:53 +0200 Subject: [PATCH 1/3] feat(jest-resolver): support `file://` URLs --- CHANGELOG.md | 1 + .../src/__tests__/resolve.test.ts | 19 +++++++++++++++++++ packages/jest-resolve/src/defaultResolver.ts | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b33270d10468..1db4dd44e2cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - `[jest-matcher-utils]` Add `SERIALIZABLE_PROPERTIES` to allow custom serialization of objects ([#14893](https://github.com/jestjs/jest/pull/14893)) - `[jest-mock]` Add support for the Explicit Resource Management proposal to use the `using` keyword with `jest.spyOn(object, methodName)` ([#14895](https://github.com/jestjs/jest/pull/14895)) - `[jest-reporters]` Add support for [DEC mode 2026](https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036) ([#15008](https://github.com/jestjs/jest/pull/15008)) +- `[jest-resolver]` Support `file://` URLs as paths - `[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598)) - `[jest-runtime]` Support `import.meta.filename` and `import.meta.dirname` (available from [Node 20.11](https://nodejs.org/en/blog/release/v20.11.0)) ([#14854](https://github.com/jestjs/jest/pull/14854)) - `[jest-runtime]` Support `import.meta.resolve` ([#14930](https://github.com/jestjs/jest/pull/14930)) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index b7408dc8cd07..df3096e927e5 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -7,6 +7,7 @@ */ import * as path from 'path'; +import {fileURLToPath, pathToFileURL} from 'url'; import * as fs from 'graceful-fs'; import {sync as resolveSync} from 'resolve'; import {type IModuleMap, ModuleMap} from 'jest-haste-map'; @@ -152,6 +153,15 @@ describe('findNodeModule', () => { ); }); + it('supports file URLs', () => { + const path = pathToFileURL(__filename).href; + const newPath = Resolver.findNodeModule(path, { + basedir: '/', + }); + + expect(newPath).toBe(__filename); + }); + describe('conditions', () => { const conditionsRoot = path.resolve(__dirname, '../__mocks__/conditions'); @@ -456,6 +466,15 @@ describe('findNodeModuleAsync', () => { }), ); }); + + it('supports file URLs', async () => { + const path = pathToFileURL(__filename).href; + const newPath = await Resolver.findNodeModuleAsync(path, { + basedir: '/', + }); + + expect(newPath).toBe(__filename); + }); }); describe('resolveModule', () => { diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index b2d158baa6d3..2ee685999c3c 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -6,6 +6,7 @@ */ import {dirname, isAbsolute, resolve as pathResolve} from 'path'; +import {fileURLToPath} from 'url'; import pnpResolver from 'jest-pnp-resolver'; import { type SyncOpts as UpstreamResolveOptions, @@ -132,6 +133,10 @@ function getPathInModule( path: string, options: UpstreamResolveOptionsWithConditions, ): string { + if (path.startsWith('file://')) { + path = fileURLToPath(path); + } + if (shouldIgnoreRequestForExports(path)) { return path; } From ff4e61c5fa091ef9121f698b50519f0afc0772e4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 30 Jun 2024 14:36:20 +0200 Subject: [PATCH 2/3] link issue --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1db4dd44e2cf..6e2eba07421a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ - `[jest-matcher-utils]` Add `SERIALIZABLE_PROPERTIES` to allow custom serialization of objects ([#14893](https://github.com/jestjs/jest/pull/14893)) - `[jest-mock]` Add support for the Explicit Resource Management proposal to use the `using` keyword with `jest.spyOn(object, methodName)` ([#14895](https://github.com/jestjs/jest/pull/14895)) - `[jest-reporters]` Add support for [DEC mode 2026](https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036) ([#15008](https://github.com/jestjs/jest/pull/15008)) -- `[jest-resolver]` Support `file://` URLs as paths +- `[jest-resolver]` Support `file://` URLs as paths ([#15154](https://github.com/jestjs/jest/pull/15154)) - `[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598)) - `[jest-runtime]` Support `import.meta.filename` and `import.meta.dirname` (available from [Node 20.11](https://nodejs.org/en/blog/release/v20.11.0)) ([#14854](https://github.com/jestjs/jest/pull/14854)) - `[jest-runtime]` Support `import.meta.resolve` ([#14930](https://github.com/jestjs/jest/pull/14930)) From a0ad2f75702b4c954831c500b3a24502cac04fa8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 30 Jun 2024 15:22:45 +0200 Subject: [PATCH 3/3] snaps --- e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap | 4 ++-- e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap | 2 +- .../__snapshots__/resolveNoFileExtensions.test.ts.snap | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index c8d8b0a2d976..19bec39f956a 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -41,7 +41,7 @@ exports[`moduleNameMapper wrong array configuration 1`] = ` 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1172:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1182:17) at Object.require (index.js:10:1) at Object.require (__tests__/index.js:10:20)" `; @@ -71,7 +71,7 @@ exports[`moduleNameMapper wrong configuration 1`] = ` 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1172:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:1182:17) at Object.require (index.js:10:1) at Object.require (__tests__/index.js:10:20)" `; diff --git a/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap b/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap index 2fbdb214626f..12ed22b1f0bc 100644 --- a/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap +++ b/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap @@ -26,7 +26,7 @@ exports[`shows a proper error from deep requires 1`] = ` 12 | test('dummy', () => { 13 | expect(1).toBe(1); - at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/index.js:927:11) + at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/index.js:937:11) at Object. (node_modules/discord.js/src/index.js:21:12) at Object.require (__tests__/test.js:10:1)" `; diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap index 1372c981269c..959ff25f3038 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap @@ -37,7 +37,7 @@ exports[`show error message with matching files 1`] = ` | ^ 9 | - at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/index.js:927:11) + at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/index.js:937:11) at Object.require (index.js:8:18) at Object.require (__tests__/test.js:8:11)" `;