Skip to content

Commit

Permalink
fix: absolute paths in moduleDirectories are invalid in Windows OS (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
warren-bank authored and cpojer committed Jan 29, 2018
1 parent ffb3be8 commit 1f6e35c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 60 additions & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
18 changes: 11 additions & 7 deletions packages/jest-resolve/src/node_modules_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 1f6e35c

Please sign in to comment.