Skip to content

Commit

Permalink
fix: call custom resolver with node.js modules (#12654)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChocolateLoverRaj authored Apr 9, 2022
1 parent 09fa164 commit 62b4bd7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
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) {
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) {
return name;
}

Expand Down

0 comments on commit 62b4bd7

Please sign in to comment.