Skip to content

Commit 1f6e35c

Browse files
warren-bankcpojer
authored andcommitted
fix: absolute paths in moduleDirectories are invalid in Windows OS (#5398)
* fix #5396: preserve absolute paths in `moduleDirectories` * refactor unit tests * pretty * refactor: use mock * bug fix * CHANGELOG * adjust sample data in unit tests * adjust sample data in unit tests
1 parent ffb3be8 commit 1f6e35c

3 files changed

Lines changed: 75 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
paths. ([#5403](https://github.com/facebook/jest/pull/5403))
2525
* `[jest-resolve]` Get builtin modules from node core.
2626
([#5411](https://github.com/facebook/jest/pull/5411))
27+
* `[jest-resolve]` Detect and preserve absolute paths in `moduleDirectories`. Do
28+
not generate additional (invalid) paths by prepending each ancestor of `cwd`
29+
to the absolute path. Additionally, this fixes functionality in Windows OS.
30+
([#5398](https://github.com/facebook/jest/pull/5398))
2731

2832
## jest 22.1.4
2933

packages/jest-resolve/src/__tests__/resolve.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,63 @@ describe('nodeModulesPaths', () => {
190190
expect(result[result.length - 1]).toBe('./customFolder');
191191
});
192192
});
193+
194+
describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
195+
const _path = path;
196+
let moduleMap;
197+
198+
beforeEach(() => {
199+
jest.resetModules();
200+
201+
moduleMap = new ModuleMap({
202+
duplicates: [],
203+
map: [],
204+
mocks: [],
205+
});
206+
});
207+
208+
afterAll(() => {
209+
jest.resetModules();
210+
jest.dontMock('path');
211+
});
212+
213+
it('can resolve node modules relative to absolute paths in "moduleDirectories" on Windows platforms', () => {
214+
jest.doMock('path', () => _path.win32);
215+
const path = require('path');
216+
const Resolver = require('../');
217+
218+
const cwd = 'D:\\temp\\project';
219+
const src = 'C:\\path\\to\\node_modules';
220+
const resolver = new Resolver(moduleMap, {
221+
moduleDirectories: [src, 'node_modules'],
222+
});
223+
const dirs_expected = [
224+
src,
225+
cwd + '\\node_modules',
226+
path.dirname(cwd) + '\\node_modules',
227+
'D:\\node_modules',
228+
];
229+
const dirs_actual = resolver.getModulePaths(cwd);
230+
expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected));
231+
});
232+
233+
it('can resolve node modules relative to absolute paths in "moduleDirectories" on Posix platforms', () => {
234+
jest.doMock('path', () => _path.posix);
235+
const path = require('path');
236+
const Resolver = require('../');
237+
238+
const cwd = '/temp/project';
239+
const src = '/path/to/node_modules';
240+
const resolver = new Resolver(moduleMap, {
241+
moduleDirectories: [src, 'node_modules'],
242+
});
243+
const dirs_expected = [
244+
src,
245+
cwd + '/node_modules',
246+
path.dirname(cwd) + '/node_modules',
247+
'/node_modules',
248+
];
249+
const dirs_actual = resolver.getModulePaths(cwd);
250+
expect(dirs_actual).toEqual(expect.arrayContaining(dirs_expected));
251+
});
252+
});

packages/jest-resolve/src/node_modules_paths.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ export default function nodeModulesPaths(
4444
parsed = path.parse(parsed.dir);
4545
}
4646

47-
const dirs = paths.reduce((dirs, aPath) => {
48-
return dirs.concat(
49-
modules.map(moduleDir => {
50-
return path.join(prefix, aPath, moduleDir);
51-
}),
52-
);
53-
}, []);
47+
const dirs = paths
48+
.reduce((dirs, aPath) => {
49+
return dirs.concat(
50+
modules.map(moduleDir => {
51+
return path.isAbsolute(moduleDir)
52+
? aPath === basedirAbs ? moduleDir : ''
53+
: path.join(prefix, aPath, moduleDir);
54+
}),
55+
);
56+
}, [])
57+
.filter(dir => dir !== '');
5458

5559
return options.paths ? dirs.concat(options.paths) : dirs;
5660
}

0 commit comments

Comments
 (0)