Skip to content

chore: migrate resolve and resolve.exports to unrs-resolver #15619

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1482,10 +1482,6 @@ type ResolverOptions = {
moduleDirectory?: Array<string>;
/** List of `require.paths` to use if nothing is found in `node_modules`. */
paths?: Array<string>;
/** Allows transforming parsed `package.json` contents. */
packageFilter?: (pkg: PackageJSON, file: string, dir: string) => PackageJSON;
/** Allows transforms a path within a package. */
pathFilter?: (pkg: PackageJSON, path: string, relativePath: string) => string;
/** Current root directory. */
rootDir?: string;
};
Expand Down
10 changes: 3 additions & 7 deletions packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
"chalk": "^4.0.0",
"graceful-fs": "^4.2.9",
"jest-haste-map": "workspace:*",
"jest-pnp-resolver": "^1.2.2",
"jest-util": "workspace:*",
"jest-validate": "workspace:*",
"resolve": "^1.20.0",
"resolve.exports": "^2.0.0",
"slash": "^3.0.0"
"slash": "^3.0.0",
"unrs-resolver": "^1.7.2"
},
"devDependencies": {
"@types/graceful-fs": "^4.1.3",
"@types/pnpapi": "^0.0.5",
"@types/resolve": "^1.20.2"
"@types/graceful-fs": "^4.1.3"
},
"engines": {
"node": "^16.10.0 || ^18.12.0 || >=20.0.0"
Expand Down
73 changes: 17 additions & 56 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,32 @@
*
*/

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';
import * as path from 'path';
import {sync as resolveSync} from 'unrs-resolver';
import {pathToFileURL} from 'url';

import userResolver from '../__mocks__/userResolver';
import userResolverAsync from '../__mocks__/userResolverAsync';
import defaultResolver, {type PackageFilter} from '../defaultResolver';
import defaultResolver from '../defaultResolver';
import nodeModulesPaths from '../nodeModulesPaths';
import Resolver from '../resolver';
import type {ResolverConfig} from '../types';

jest.mock('../__mocks__/userResolver').mock('../__mocks__/userResolverAsync');

// Do not fully mock `resolve` because it is used by Jest. Doing it will crash
// Do not fully mock `unrs-resolver` because it is used by Jest. Doing it will crash
// in very strange ways. Instead, just spy on it and its `sync` method.
jest.mock('resolve', () => {
jest.mock('unrs-resolver', () => {
const originalModule =
jest.requireActual<typeof import('resolve')>('resolve');

const m = jest.fn<typeof import('resolve')>((...args) =>
originalModule(...args),
);
Object.assign(m, originalModule);
m.sync = jest.spyOn(originalModule, 'sync');

return m;
jest.requireActual<typeof import('unrs-resolver')>('unrs-resolver');
return {
...originalModule,
sync: jest
.spyOn(originalModule, 'sync')
.mockImplementation((...args) => originalModule.sync(...args)),
};
});

const mockUserResolver = jest.mocked(userResolver);
Expand Down Expand Up @@ -134,25 +133,6 @@ describe('findNodeModule', () => {
});
});

it('wraps passed packageFilter to the resolve module when using the default resolver', () => {
const packageFilter = jest.fn<PackageFilter>();

// A resolver that delegates to defaultResolver with a packageFilter implementation
mockUserResolver.mockImplementation((request, opts) =>
opts.defaultResolver(request, {...opts, packageFilter}),
);

Resolver.findNodeModule('./test', {
basedir: path.resolve(__dirname, '../__mocks__/'),
resolver: require.resolve('../__mocks__/userResolver'),
});

expect(packageFilter).toHaveBeenCalledWith(
expect.objectContaining({name: '__mocks__'}),
expect.any(String),
);
});

it('supports file URLs', () => {
const path = pathToFileURL(__filename).href;
const newPath = Resolver.findNodeModule(path, {
Expand Down Expand Up @@ -407,7 +387,9 @@ describe('findNodeModule', () => {
basedir: path.resolve(importsRoot, './foo-import/index.js'),
conditions: [],
});
}).toThrow('Missing "#something-else" specifier in "foo-import" package');
}).toThrow(
`Package import specifier "#something-else" is not defined in package ${path.join(importsRoot, 'foo-import/package.json')}`,
);
});
});
});
Expand Down Expand Up @@ -446,27 +428,6 @@ describe('findNodeModuleAsync', () => {
});
});

it('passes packageFilter to the resolve module when using the default resolver', async () => {
const packageFilter = jest.fn<PackageFilter>();

// A resolver that delegates to defaultResolver with a packageFilter implementation
mockUserResolverAsync.async.mockImplementation((request, opts) =>
Promise.resolve(opts.defaultResolver(request, {...opts, packageFilter})),
);

await Resolver.findNodeModuleAsync('test', {
basedir: '/',
resolver: require.resolve('../__mocks__/userResolverAsync'),
});

expect(mockResolveSync).toHaveBeenCalledWith(
'test',
expect.objectContaining({
packageFilter,
}),
);
});

it('supports file URLs', async () => {
const path = pathToFileURL(__filename).href;
const newPath = await Resolver.findNodeModuleAsync(path, {
Expand Down
Loading
Loading