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

feat(jest-resolver): support file:// URLs #15154

Merged
merged 4 commits into from
Jun 30, 2024
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 @@ -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 ([#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))
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
`;
Expand Down Expand Up @@ -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)"
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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.<anonymous> (node_modules/discord.js/src/index.js:21:12)
at Object.require (__tests__/test.js:10:1)"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
`;
19 changes: 19 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -132,6 +133,10 @@ function getPathInModule(
path: string,
options: UpstreamResolveOptionsWithConditions,
): string {
if (path.startsWith('file://')) {
path = fileURLToPath(path);
}

if (shouldIgnoreRequestForExports(path)) {
return path;
}
Expand Down
Loading