Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: absolute paths in moduleDirectories are invalid in Windows OS #5398

Merged
merged 9 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
* `[jest-resolve]` Search required modules in node_modules and then in custom
paths.
([#5403](https://github.com/facebook/jest/pull/5403))
* `[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;
}