Skip to content

Commit

Permalink
fix: moduleNameMapper should take precedence over Node core modules (
Browse files Browse the repository at this point in the history
  • Loading branch information
lh0x00 authored Feb 12, 2020
1 parent 14e3992 commit 2793c67
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499))
- `[jest-resolve]` Fix module identity preservation with symlinks and browser field resolution ([#9511](https://github.com/facebook/jest/pull/9511))
- `[jest-resolve]` Do not confuse directories with files ([#8912](https://github.com/facebook/jest/pull/8912))
- `[jest-resolve]` `moduleNameMapper` should take precedence over Node core modules ([#9563](https://github.com/facebook/jest/pull/9563))
- `[jest-runtime]` Reset `isolateModules` if it fails ([#9541](https://github.com/facebook/jest/pull/9541))
- `[jest-snapshot]` Downgrade semver to v6 to support node 8 ([#9451](https://github.com/facebook/jest/pull/9451))
- `[jest-snapshot]` Properly indent new snapshots in the presences of existing ones ([#9523](https://github.com/facebook/jest/pull/9523))
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 @@ -36,7 +36,7 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:507:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:519:17)
at Object.require (index.js:10:1)
`;

Expand Down Expand Up @@ -65,6 +65,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:507:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:519:17)
at Object.require (index.js:10:1)
`;
14 changes: 14 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ describe('isCoreModule', () => {
const isCore = resolver.isCoreModule('not-a-core-module');
expect(isCore).toEqual(false);
});

it('returns false if `hasCoreModules` is true and `moduleNameMapper` alias a module same name with core module', () => {
const moduleMap = ModuleMap.create('/');
const resolver = new Resolver(moduleMap, {
moduleNameMapper: [
{
moduleName: '$1',
regex: /^constants$/,
},
],
} as ResolverConfig);
const isCore = resolver.isCoreModule('constants');
expect(isCore).toEqual(false);
});
});

describe('findNodeModule', () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/jest-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,21 @@ class Resolver {
);
}

private _isAliasModule(moduleName: string): boolean {
const moduleNameMapper = this._options.moduleNameMapper;
if (!moduleNameMapper) {
return false;
}

return moduleNameMapper.some(({regex}) => regex.test(moduleName));
}

isCoreModule(moduleName: string): boolean {
return this._options.hasCoreModules && isBuiltinModule(moduleName);
return (
this._options.hasCoreModules &&
isBuiltinModule(moduleName) &&
!this._isAliasModule(moduleName)
);
}

getModule(name: string): Config.Path | null {
Expand Down

0 comments on commit 2793c67

Please sign in to comment.