From 2793c67e36e245a2832fa2c39123e1ec507023a2 Mon Sep 17 00:00:00 2001 From: Hieu Lam Date: Wed, 12 Feb 2020 15:01:59 +0700 Subject: [PATCH] fix: `moduleNameMapper` should take precedence over Node core modules (#9563) --- CHANGELOG.md | 1 + .../__snapshots__/moduleNameMapper.test.ts.snap | 4 ++-- .../jest-resolve/src/__tests__/resolve.test.ts | 14 ++++++++++++++ packages/jest-resolve/src/index.ts | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a313de73be80..e0efdc0b12c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index b7f122ffdc54..0ea10ceef1a3 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -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) `; @@ -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) `; diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 4d8d174385a7..9e5499c5832f 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -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', () => { diff --git a/packages/jest-resolve/src/index.ts b/packages/jest-resolve/src/index.ts index 4020a62a7c55..ec6c3ab48657 100644 --- a/packages/jest-resolve/src/index.ts +++ b/packages/jest-resolve/src/index.ts @@ -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 {