From 46da2047656926dae0dbde22b62e93933c1e43f0 Mon Sep 17 00:00:00 2001 From: lvqq Date: Tue, 22 Nov 2022 00:18:00 +0800 Subject: [PATCH 1/9] fix: add global path in require.resolve.paths --- .../src/__tests__/resolve.test.ts | 32 +++++++++++++++++++ packages/jest-resolve/src/resolver.ts | 26 +++++++++++++++ packages/jest-runtime/src/index.ts | 22 ++++++++++--- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 2b85fe4aaa7c..0deb0fb025e6 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -707,3 +707,35 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected)); }); }); + +describe('Resolver.getGlobalPaths()', () => { + const _path = path; + let moduleMap: IModuleMap; + beforeEach(() => { + moduleMap = ModuleMap.create('/'); + }); + + it('return global paths with npm package', () => { + jest.doMock('path', () => _path.posix); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); + const cwd = '/temp/project'; + const globalPaths = resolver.getGlobalPaths(cwd, 'jest'); + expect(globalPaths.length).toBeGreaterThanOrEqual(1); + }); + + it('return empty with relative path', () => { + jest.doMock('path', () => _path.posix); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); + const cwd = '/temp/project'; + const globalPaths = resolver.getGlobalPaths(cwd, './module'); + expect(globalPaths).toStrictEqual([]); + }); + + it('return empty without module name', () => { + jest.doMock('path', () => _path.posix); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); + const cwd = '/temp/project'; + const globalPaths = resolver.getGlobalPaths(cwd); + expect(globalPaths).toStrictEqual([]); + }); +}); diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index 98dbfc916ad7..3f53ece9c3e7 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -59,6 +59,7 @@ export default class Resolver { private readonly _moduleIDCache: Map; private readonly _moduleNameCache: Map; private readonly _modulePathCache: Map>; + private readonly _globalPathCache: Map>; private readonly _supportsNativePlatform: boolean; constructor(moduleMap: IModuleMap, options: ResolverConfig) { @@ -81,6 +82,7 @@ export default class Resolver { this._moduleIDCache = new Map(); this._moduleNameCache = new Map(); this._modulePathCache = new Map(); + this._globalPathCache = new Map(); } static ModuleNotFoundError = ModuleNotFoundError; @@ -526,6 +528,30 @@ export default class Resolver { return paths; } + getGlobalPaths(from: string, moduleName?: string): Array { + if (!moduleName || moduleName[0] === '.') return []; + + const cachedGlobalPath = this._globalPathCache.get(moduleName); + if (cachedGlobalPath) { + return cachedGlobalPath; + } + + const modulePaths = this.getModulePaths(from); + const globalPaths: Array = []; + + const paths = require.resolve.paths(moduleName); + if (paths) { + // find the rootIndex and remain the global paths + const rootIndex = paths.findIndex( + path => path === modulePaths[modulePaths.length - 1], + ); + globalPaths.push(...(rootIndex > -1 ? paths.slice(rootIndex + 1) : [])); + } + + this._globalPathCache.set(moduleName, globalPaths); + return globalPaths; + } + getModuleID( virtualMocks: Map, from: string, diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 5dcf6d7ce6c8..7e1afae1ab7f 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1066,7 +1066,13 @@ export default class Runtime { } else { // Only include the fromPath if a moduleName is given. Else treat as root. const fromPath = moduleName ? from : null; - this._execModule(localModule, options, moduleRegistry, fromPath); + this._execModule( + localModule, + options, + moduleRegistry, + fromPath, + moduleName, + ); } localModule.loaded = true; } @@ -1398,6 +1404,7 @@ export default class Runtime { } private _requireResolvePaths(from: string, moduleName?: string) { + const fromDir = path.resolve(from, '..'); if (moduleName == null) { throw new Error( 'The first argument to require.resolve.paths must be a string. Received null or undefined.', @@ -1410,12 +1417,14 @@ export default class Runtime { } if (moduleName[0] === '.') { - return [path.resolve(from, '..')]; + return [fromDir]; } if (this._resolver.isCoreModule(moduleName)) { return null; } - return this._resolver.getModulePaths(path.resolve(from, '..')); + const modulePaths = this._resolver.getModulePaths(fromDir); + const globalPaths = this._resolver.getGlobalPaths(fromDir, moduleName); + return [...modulePaths, ...globalPaths]; } private _execModule( @@ -1423,6 +1432,7 @@ export default class Runtime { options: InternalModuleOptions | undefined, moduleRegistry: ModuleRegistry, from: string | null, + moduleName?: string, ) { if (this.isTornDown) { this._logFormattedReferenceError( @@ -1454,8 +1464,10 @@ export default class Runtime { return moduleRegistry.get(key) || null; }, }); - - module.paths = this._resolver.getModulePaths(module.path); + module.paths = [ + ...this._resolver.getModulePaths(module.path), + ...this._resolver.getGlobalPaths(module.path, moduleName), + ]; Object.defineProperty(module, 'require', { value: this._createRequireImplementation(module, options), }); From c96b30086d321534de56fcfb96cfd41a3ef0b622 Mon Sep 17 00:00:00 2001 From: lvqq Date: Tue, 22 Nov 2022 00:54:45 +0800 Subject: [PATCH 2/9] test: update snapshot --- e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap | 4 ++-- e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index 5e4d71fbf3e9..e3eefeb40930 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -41,7 +41,7 @@ exports[`moduleNameMapper wrong array configuration 1`] = ` 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:752:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:773:17) at Object.require (index.js:10:1) at Object.require (__tests__/index.js:10:20)" `; @@ -71,7 +71,7 @@ exports[`moduleNameMapper wrong configuration 1`] = ` 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:752:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:773:17) at Object.require (index.js:10:1) at Object.require (__tests__/index.js:10:20)" `; diff --git a/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap b/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap index 62cd96c39c42..34f55ffd75e3 100644 --- a/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap +++ b/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap @@ -26,7 +26,7 @@ exports[`shows a proper error from deep requires 1`] = ` 12 | test('dummy', () => { 13 | expect(1).toBe(1); - at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:425:11) + at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:427:11) at Object. (node_modules/discord.js/src/index.js:21:12) at Object.require (__tests__/test.js:10:1)" `; From 6dc8310a7a3672b20253aba59ff9f0403e5e7c7c Mon Sep 17 00:00:00 2001 From: lvqq Date: Tue, 22 Nov 2022 01:08:28 +0800 Subject: [PATCH 3/9] test: update snapshot --- .../__snapshots__/resolveNoFileExtensions.test.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap index a6c967952cb4..98867dc0fce7 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap @@ -37,7 +37,7 @@ exports[`show error message with matching files 1`] = ` | ^ 9 | - at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:425:11) + at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:427:11) at Object.require (index.js:8:18) at Object.require (__tests__/test.js:8:11)" `; From 349dd7dbfc8b76f29a30499380c9cfc8f89f6b59 Mon Sep 17 00:00:00 2001 From: lvqq Date: Wed, 23 Nov 2022 11:37:16 +0800 Subject: [PATCH 4/9] test: add builtin module test case --- packages/jest-resolve/src/__tests__/resolve.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 0deb0fb025e6..7676dd5d5db0 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -723,7 +723,15 @@ describe('Resolver.getGlobalPaths()', () => { expect(globalPaths.length).toBeGreaterThanOrEqual(1); }); - it('return empty with relative path', () => { + it('return empty array with builtin module', () => { + jest.doMock('path', () => _path.posix); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); + const cwd = '/temp/project'; + const globalPaths = resolver.getGlobalPaths(cwd, 'fs'); + expect(globalPaths).toStrictEqual([]); + }); + + it('return empty array with relative path', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); const cwd = '/temp/project'; @@ -731,7 +739,7 @@ describe('Resolver.getGlobalPaths()', () => { expect(globalPaths).toStrictEqual([]); }); - it('return empty without module name', () => { + it('return empty array without module name', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); const cwd = '/temp/project'; From 4ae19b984d9be958730bfdf0924fe38ac0c08166 Mon Sep 17 00:00:00 2001 From: lvqq Date: Thu, 24 Nov 2022 22:13:26 +0800 Subject: [PATCH 5/9] refactor: grap global paths only once --- packages/jest-resolve/src/nodeModulesPaths.ts | 17 +++++++++++ packages/jest-resolve/src/resolver.ts | 28 ++++--------------- packages/jest-runtime/src/index.ts | 10 +++---- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/packages/jest-resolve/src/nodeModulesPaths.ts b/packages/jest-resolve/src/nodeModulesPaths.ts index a2e91914de66..50778da4a980 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.ts +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -70,3 +70,20 @@ export default function nodeModulesPaths( return options.paths ? dirs.concat(options.paths) : dirs; } + +export function findGlobalPaths(): Array { + const parsed = path.parse(process.cwd()); + const prefix = parsed.root; + const resolvePaths = require.resolve.paths('/'); + + if (resolvePaths) { + // find the rootIndex and remain the global paths + const rootIndex = resolvePaths.findIndex( + resolvePath => resolvePath === `${prefix}node_modules`, + ); + return rootIndex > -1 ? resolvePaths.slice(rootIndex + 1) : []; + } + return []; +} + +export const GlobalPaths = findGlobalPaths(); diff --git a/packages/jest-resolve/src/resolver.ts b/packages/jest-resolve/src/resolver.ts index 3f53ece9c3e7..9318b3b4059c 100644 --- a/packages/jest-resolve/src/resolver.ts +++ b/packages/jest-resolve/src/resolver.ts @@ -20,7 +20,7 @@ import defaultResolver, { } from './defaultResolver'; import {clearFsCache} from './fileWalkers'; import isBuiltinModule from './isBuiltinModule'; -import nodeModulesPaths from './nodeModulesPaths'; +import nodeModulesPaths, {GlobalPaths} from './nodeModulesPaths'; import shouldLoadAsEsm, {clearCachedLookups} from './shouldLoadAsEsm'; import type {ResolverConfig} from './types'; @@ -59,7 +59,6 @@ export default class Resolver { private readonly _moduleIDCache: Map; private readonly _moduleNameCache: Map; private readonly _modulePathCache: Map>; - private readonly _globalPathCache: Map>; private readonly _supportsNativePlatform: boolean; constructor(moduleMap: IModuleMap, options: ResolverConfig) { @@ -82,7 +81,6 @@ export default class Resolver { this._moduleIDCache = new Map(); this._moduleNameCache = new Map(); this._modulePathCache = new Map(); - this._globalPathCache = new Map(); } static ModuleNotFoundError = ModuleNotFoundError; @@ -528,28 +526,12 @@ export default class Resolver { return paths; } - getGlobalPaths(from: string, moduleName?: string): Array { - if (!moduleName || moduleName[0] === '.') return []; - - const cachedGlobalPath = this._globalPathCache.get(moduleName); - if (cachedGlobalPath) { - return cachedGlobalPath; - } - - const modulePaths = this.getModulePaths(from); - const globalPaths: Array = []; - - const paths = require.resolve.paths(moduleName); - if (paths) { - // find the rootIndex and remain the global paths - const rootIndex = paths.findIndex( - path => path === modulePaths[modulePaths.length - 1], - ); - globalPaths.push(...(rootIndex > -1 ? paths.slice(rootIndex + 1) : [])); + getGlobalPaths(moduleName?: string): Array { + if (!moduleName || moduleName[0] === '.' || this.isCoreModule(moduleName)) { + return []; } - this._globalPathCache.set(moduleName, globalPaths); - return globalPaths; + return GlobalPaths; } getModuleID( diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 7e1afae1ab7f..c278d77052c5 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1423,7 +1423,7 @@ export default class Runtime { return null; } const modulePaths = this._resolver.getModulePaths(fromDir); - const globalPaths = this._resolver.getGlobalPaths(fromDir, moduleName); + const globalPaths = this._resolver.getGlobalPaths(moduleName); return [...modulePaths, ...globalPaths]; } @@ -1464,10 +1464,10 @@ export default class Runtime { return moduleRegistry.get(key) || null; }, }); - module.paths = [ - ...this._resolver.getModulePaths(module.path), - ...this._resolver.getGlobalPaths(module.path, moduleName), - ]; + const modulePaths = this._resolver.getModulePaths(module.path); + const globalPaths = this._resolver.getGlobalPaths(moduleName); + module.paths = [...modulePaths, ...globalPaths]; + Object.defineProperty(module, 'require', { value: this._createRequireImplementation(module, options), }); From b010ad2f65e5092cd470889cff82ac7c3c65f0c0 Mon Sep 17 00:00:00 2001 From: lvqq Date: Thu, 24 Nov 2022 22:15:46 +0800 Subject: [PATCH 6/9] test: update cases and snapshot --- .../moduleNameMapper.test.ts.snap | 4 +-- .../requireMissingExt.test.ts.snap | 2 +- .../resolveNoFileExtensions.test.ts.snap | 2 +- .../src/__tests__/resolve.test.ts | 26 ++++++++++++------- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index e3eefeb40930..9a78de751fe5 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -41,7 +41,7 @@ exports[`moduleNameMapper wrong array configuration 1`] = ` 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:773:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:758:17) at Object.require (index.js:10:1) at Object.require (__tests__/index.js:10:20)" `; @@ -71,7 +71,7 @@ exports[`moduleNameMapper wrong configuration 1`] = ` 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:773:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/resolver.js:758:17) at Object.require (index.js:10:1) at Object.require (__tests__/index.js:10:20)" `; diff --git a/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap b/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap index 34f55ffd75e3..62cd96c39c42 100644 --- a/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap +++ b/e2e/__tests__/__snapshots__/requireMissingExt.test.ts.snap @@ -26,7 +26,7 @@ exports[`shows a proper error from deep requires 1`] = ` 12 | test('dummy', () => { 13 | expect(1).toBe(1); - at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:427:11) + at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:425:11) at Object. (node_modules/discord.js/src/index.js:21:12) at Object.require (__tests__/test.js:10:1)" `; diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap index 98867dc0fce7..a6c967952cb4 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap @@ -37,7 +37,7 @@ exports[`show error message with matching files 1`] = ` | ^ 9 | - at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:427:11) + at Resolver._throwModNotFoundError (../../packages/jest-resolve/build/resolver.js:425:11) at Object.require (index.js:8:18) at Object.require (__tests__/test.js:8:11)" `; diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 7676dd5d5db0..4c7f5bc960de 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -13,7 +13,7 @@ import {IModuleMap, ModuleMap} from 'jest-haste-map'; import userResolver from '../__mocks__/userResolver'; import userResolverAsync from '../__mocks__/userResolverAsync'; import defaultResolver, {PackageFilter} from '../defaultResolver'; -import nodeModulesPaths from '../nodeModulesPaths'; +import nodeModulesPaths, {findGlobalPaths} from '../nodeModulesPaths'; import Resolver from '../resolver'; import type {ResolverConfig} from '../types'; @@ -708,6 +708,11 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { }); }); +describe('findGlobalPaths', () => { + const paths = findGlobalPaths(); + expect(paths.length).toBeGreaterThanOrEqual(1); +}); + describe('Resolver.getGlobalPaths()', () => { const _path = path; let moduleMap: IModuleMap; @@ -718,32 +723,35 @@ describe('Resolver.getGlobalPaths()', () => { it('return global paths with npm package', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); - const cwd = '/temp/project'; - const globalPaths = resolver.getGlobalPaths(cwd, 'jest'); + const globalPaths = resolver.getGlobalPaths('jest'); expect(globalPaths.length).toBeGreaterThanOrEqual(1); }); it('return empty array with builtin module', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); - const cwd = '/temp/project'; - const globalPaths = resolver.getGlobalPaths(cwd, 'fs'); + const globalPaths = resolver.getGlobalPaths('fs'); expect(globalPaths).toStrictEqual([]); }); + it('return empty array with absolute path', () => { + jest.doMock('path', () => _path.posix); + const resolver = new Resolver(moduleMap, {} as ResolverConfig); + const globalPaths = resolver.getGlobalPaths('/'); + expect(globalPaths.length).toBeGreaterThanOrEqual(1); + }); + it('return empty array with relative path', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); - const cwd = '/temp/project'; - const globalPaths = resolver.getGlobalPaths(cwd, './module'); + const globalPaths = resolver.getGlobalPaths('./'); expect(globalPaths).toStrictEqual([]); }); it('return empty array without module name', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); - const cwd = '/temp/project'; - const globalPaths = resolver.getGlobalPaths(cwd); + const globalPaths = resolver.getGlobalPaths(); expect(globalPaths).toStrictEqual([]); }); }); From 1c88b5c3d31ef7330e6e23fe172c35ac0b92fc26 Mon Sep 17 00:00:00 2001 From: lvqq Date: Thu, 24 Nov 2022 22:54:16 +0800 Subject: [PATCH 7/9] chore: add path join and update test case --- packages/jest-resolve/src/__tests__/resolve.test.ts | 4 ++-- packages/jest-resolve/src/nodeModulesPaths.ts | 3 ++- .../src/__tests__/runtime_require_module.test.js | 9 +++++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 4c7f5bc960de..9a33c9c74355 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -724,7 +724,7 @@ describe('Resolver.getGlobalPaths()', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); const globalPaths = resolver.getGlobalPaths('jest'); - expect(globalPaths.length).toBeGreaterThanOrEqual(1); + expect(globalPaths.length).toBeGreaterThan(0); }); it('return empty array with builtin module', () => { @@ -738,7 +738,7 @@ describe('Resolver.getGlobalPaths()', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); const globalPaths = resolver.getGlobalPaths('/'); - expect(globalPaths.length).toBeGreaterThanOrEqual(1); + expect(globalPaths.length).toBeGreaterThan(0); }); it('return empty array with relative path', () => { diff --git a/packages/jest-resolve/src/nodeModulesPaths.ts b/packages/jest-resolve/src/nodeModulesPaths.ts index 50778da4a980..9827dd4ffc8d 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.ts +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -74,12 +74,13 @@ export default function nodeModulesPaths( export function findGlobalPaths(): Array { const parsed = path.parse(process.cwd()); const prefix = parsed.root; + const globalPath = path.join(prefix, 'node_modules'); const resolvePaths = require.resolve.paths('/'); if (resolvePaths) { // find the rootIndex and remain the global paths const rootIndex = resolvePaths.findIndex( - resolvePath => resolvePath === `${prefix}node_modules`, + resolvePath => resolvePath === globalPath, ); return rootIndex > -1 ? resolvePaths.slice(rootIndex + 1) : []; } diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js index c2614d7563da..e28b2fcdc5e2 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module.test.js @@ -132,8 +132,13 @@ describe('Runtime requireModule', () => { 'RegularModule', ); expect(exports.paths.length).toBeGreaterThan(0); - exports.paths.forEach(path => { - expect(moduleDirectories.some(dir => path.endsWith(dir))).toBe(true); + const root = path.parse(process.cwd()).root; + const globalPath = path.join(root, 'node_modules'); + const rootIndex = exports.paths.findIndex(path => path === globalPath); + exports.paths.forEach((path, index) => { + if (index <= rootIndex) { + expect(moduleDirectories.some(dir => path.endsWith(dir))).toBe(true); + } }); }); From c026cd9d46c5c88408815f8c1a295c1c0b6d7c78 Mon Sep 17 00:00:00 2001 From: lvqq Date: Mon, 28 Nov 2022 23:03:07 +0800 Subject: [PATCH 8/9] test: update test cases --- .../jest-resolve/src/__tests__/resolve.test.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index 9a33c9c74355..bd3937ace51f 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -708,11 +708,6 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { }); }); -describe('findGlobalPaths', () => { - const paths = findGlobalPaths(); - expect(paths.length).toBeGreaterThanOrEqual(1); -}); - describe('Resolver.getGlobalPaths()', () => { const _path = path; let moduleMap: IModuleMap; @@ -724,7 +719,9 @@ describe('Resolver.getGlobalPaths()', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); const globalPaths = resolver.getGlobalPaths('jest'); - expect(globalPaths.length).toBeGreaterThan(0); + globalPaths.forEach(globalPath => + expect(require.resolve.paths('jest')).toContain(globalPath), + ); }); it('return empty array with builtin module', () => { @@ -734,11 +731,13 @@ describe('Resolver.getGlobalPaths()', () => { expect(globalPaths).toStrictEqual([]); }); - it('return empty array with absolute path', () => { + it('return global paths with absolute path', () => { jest.doMock('path', () => _path.posix); const resolver = new Resolver(moduleMap, {} as ResolverConfig); const globalPaths = resolver.getGlobalPaths('/'); - expect(globalPaths.length).toBeGreaterThan(0); + globalPaths.forEach(globalPath => + expect(require.resolve.paths('/')).toContain(globalPath), + ); }); it('return empty array with relative path', () => { From d27aa8b34ebe906259e1124969279b386a702ec2 Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Tue, 29 Nov 2022 14:23:21 +0100 Subject: [PATCH 9/9] clarify and add changelog --- CHANGELOG.md | 2 ++ .../jest-resolve/src/__tests__/resolve.test.ts | 2 +- packages/jest-resolve/src/nodeModulesPaths.ts | 14 +++++--------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 413fb973a41b..07f7984aa970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-resolve]` add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633)) + ### Chore & Maintenance - `[@jest/fake-timers]` Update `@sinonjs/fake-timers` ([#13612](https://github.com/facebook/jest/pull/13612)) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index bd3937ace51f..bad6fe4a3cd5 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -13,7 +13,7 @@ import {IModuleMap, ModuleMap} from 'jest-haste-map'; import userResolver from '../__mocks__/userResolver'; import userResolverAsync from '../__mocks__/userResolverAsync'; import defaultResolver, {PackageFilter} from '../defaultResolver'; -import nodeModulesPaths, {findGlobalPaths} from '../nodeModulesPaths'; +import nodeModulesPaths from '../nodeModulesPaths'; import Resolver from '../resolver'; import type {ResolverConfig} from '../types'; diff --git a/packages/jest-resolve/src/nodeModulesPaths.ts b/packages/jest-resolve/src/nodeModulesPaths.ts index 9827dd4ffc8d..1eb73efa678f 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.ts +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -71,20 +71,16 @@ export default function nodeModulesPaths( return options.paths ? dirs.concat(options.paths) : dirs; } -export function findGlobalPaths(): Array { - const parsed = path.parse(process.cwd()); - const prefix = parsed.root; - const globalPath = path.join(prefix, 'node_modules'); +function findGlobalPaths(): Array { + const {root} = path.parse(process.cwd()); + const globalPath = path.join(root, 'node_modules'); const resolvePaths = require.resolve.paths('/'); if (resolvePaths) { - // find the rootIndex and remain the global paths - const rootIndex = resolvePaths.findIndex( - resolvePath => resolvePath === globalPath, - ); + // the global paths start one after the root node_modules + const rootIndex = resolvePaths.indexOf(globalPath); return rootIndex > -1 ? resolvePaths.slice(rootIndex + 1) : []; } return []; } - export const GlobalPaths = findGlobalPaths();