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

fix: call custom resolver with node.js modules #12654

Merged
merged 4 commits into from
Apr 9, 2022
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 @@ -77,6 +77,7 @@
- `[jest-mock]` Fix function overloads for `spyOn` to allow more correct type inference in complex object ([#12442](https://github.com/facebook/jest/pull/12442))
- `[jest-reporters]` Notifications generated by the `--notify` flag are no longer persistent in GNOME Shell. ([#11733](https://github.com/facebook/jest/pull/11733))
- `[@jest/reporters]` Move missing icon file which is needed for `NotifyReporter` class. ([#12593](https://github.com/facebook/jest/pull/12593))
- `[jest-resolver]` Call custom resolver with core node.js modules ([#12654](https://github.com/facebook/jest/pull/12654))
- `[jest-worker]` Fix `Farm` execution results memory leak ([#12497](https://github.com/facebook/jest/pull/12497))

### Chore & Maintenance
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/resolver.js:899:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:901:17)
at Object.require (index.js:10:1)"
`;

Expand Down Expand Up @@ -70,6 +70,6 @@ exports[`moduleNameMapper wrong configuration 1`] = `
12 | module.exports = () => 'test';
13 |

at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:899:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:901:17)
at Object.require (index.js:10:1)"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ exports[`show error message with matching files 1`] = `
| ^
9 |

at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:491:11)
at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:493:11)
at Object.require (index.js:8:18)"
`;
15 changes: 15 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ describe('resolveModule', () => {
expect(resolvedWithSlash).toBe(fooSlashIndex);
expect(resolvedWithSlash).toBe(resolvedWithDot);
});

it('custom resolver can resolve node modules', () => {
userResolver.mockImplementation(() => 'module');

const moduleMap = ModuleMap.create('/');
const resolver = new Resolver(moduleMap, {
extensions: ['.js'],
resolver: require.resolve('../__mocks__/userResolver'),
} as ResolverConfig);
const src = require.resolve('../');
resolver.resolveModule(src, 'fs');

expect(userResolver).toHaveBeenCalled();
expect(userResolver.mock.calls[0][0]).toBe('fs');
});
});

describe('resolveModuleAsync', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-resolve/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ export default class Resolver {
// dependency graph because we don't have to look at modules that may not
// exist and aren't mocked.
const resolveNodeModule = (name: string, throwIfNotFound = false) => {
if (this.isCoreModule(name)) {
// Only skip default resolver
if (this.isCoreModule(name) && !this._options.resolver) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
return name;
}

Expand Down Expand Up @@ -292,7 +293,8 @@ export default class Resolver {
// dependency graph because we don't have to look at modules that may not
// exist and aren't mocked.
const resolveNodeModule = async (name: string, throwIfNotFound = false) => {
if (this.isCoreModule(name)) {
// Only skip default resolver
if (this.isCoreModule(name) && !this._options.resolver) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
return name;
}

Expand Down