diff --git a/CHANGELOG.md b/CHANGELOG.md index d1f6d78038a1..4127b5aa50c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,10 @@ paths. ([#5403](https://github.com/facebook/jest/pull/5403)) * `[jest-resolve]` Get builtin modules from node core. ([#5411](https://github.com/facebook/jest/pull/5411)) +* `[jest-resolve]` Detect and preserve absolute paths in `moduleDirectories`. Do + not generate additional (invalid) paths by prepending each ancestor of `cwd` + to the absolute path. Additionally, this fixes functionality in Windows OS. + ([#5398](https://github.com/facebook/jest/pull/5398)) ## jest 22.1.4 diff --git a/packages/jest-resolve/src/__tests__/resolve.test.js b/packages/jest-resolve/src/__tests__/resolve.test.js index 892f2387463a..7806495d8ee9 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.js +++ b/packages/jest-resolve/src/__tests__/resolve.test.js @@ -190,3 +190,63 @@ describe('nodeModulesPaths', () => { expect(result[result.length - 1]).toBe('./customFolder'); }); }); + +describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { + const _path = path; + let moduleMap; + + beforeEach(() => { + jest.resetModules(); + + moduleMap = new ModuleMap({ + duplicates: [], + map: [], + mocks: [], + }); + }); + + afterAll(() => { + jest.resetModules(); + jest.dontMock('path'); + }); + + it('can resolve node modules relative to absolute paths in "moduleDirectories" on Windows platforms', () => { + jest.doMock('path', () => _path.win32); + const path = require('path'); + const Resolver = require('../'); + + const cwd = 'D:\\temp\\project'; + const src = 'C:\\path\\to\\node_modules'; + const resolver = new Resolver(moduleMap, { + moduleDirectories: [src, 'node_modules'], + }); + const dirs_expected = [ + src, + cwd + '\\node_modules', + path.dirname(cwd) + '\\node_modules', + 'D:\\node_modules', + ]; + const dirs_actual = resolver.getModulePaths(cwd); + expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected)); + }); + + it('can resolve node modules relative to absolute paths in "moduleDirectories" on Posix platforms', () => { + jest.doMock('path', () => _path.posix); + const path = require('path'); + const Resolver = require('../'); + + const cwd = '/temp/project'; + const src = '/path/to/node_modules'; + const resolver = new Resolver(moduleMap, { + moduleDirectories: [src, 'node_modules'], + }); + const dirs_expected = [ + src, + cwd + '/node_modules', + path.dirname(cwd) + '/node_modules', + '/node_modules', + ]; + const dirs_actual = resolver.getModulePaths(cwd); + expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected)); + }); +}); diff --git a/packages/jest-resolve/src/node_modules_paths.js b/packages/jest-resolve/src/node_modules_paths.js index e925b629246a..21383dca2d52 100644 --- a/packages/jest-resolve/src/node_modules_paths.js +++ b/packages/jest-resolve/src/node_modules_paths.js @@ -44,13 +44,17 @@ export default function nodeModulesPaths( parsed = path.parse(parsed.dir); } - const dirs = paths.reduce((dirs, aPath) => { - return dirs.concat( - modules.map(moduleDir => { - return path.join(prefix, aPath, moduleDir); - }), - ); - }, []); + const dirs = paths + .reduce((dirs, aPath) => { + return dirs.concat( + modules.map(moduleDir => { + return path.isAbsolute(moduleDir) + ? aPath === basedirAbs ? moduleDir : '' + : path.join(prefix, aPath, moduleDir); + }), + ); + }, []) + .filter(dir => dir !== ''); return options.paths ? dirs.concat(options.paths) : dirs; }