Skip to content

fix #5396: preserve absolute paths in moduleDirectories #5408

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 absolute paths in `moduleDirectories`. Do not generate
additional (invalid) paths by prepending each ancestor of `cwd` to the
absolute path. Additionally, this patch fixes the functionality in Windows OS.
([#5398](https://github.com/facebook/jest/pull/5398))

## jest 22.1.4

Expand Down
65 changes: 64 additions & 1 deletion packages/jest-resolve/src/__tests__/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ jest.mock('../__mocks__/userResolver');
const fs = require('fs');
const path = require('path');
const ModuleMap = require('jest-haste-map').ModuleMap;
const Resolver = require('../');
const userResolver = require('../__mocks__/userResolver');
const nodeModulesPaths = require('../node_modules_paths').default;

let Resolver = require('../');

beforeEach(() => {
userResolver.mockClear();
});
Expand Down Expand Up @@ -190,3 +191,65 @@ describe('nodeModulesPaths', () => {
expect(result[result.length - 1]).toBe('./customFolder');
});
});

describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
let moduleMap;

beforeAll(() => {
jest.resetModules();
});

beforeEach(() => {
moduleMap = new ModuleMap({
duplicates: [],
map: [],
mocks: [],
});
});

afterEach(() => {
jest.resetModules();
});

afterAll(() => {
jest.dontMock('path');
Resolver = require('../');
});

it('can resolve node modules relative to absolute paths in "moduleDirectories" on Windows platforms', () => {
jest.doMock('path', () => path.win32);
Resolver = require('../');

const cwd = 'D:\\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.win32.dirname(cwd).replace(/\\$/, '') + '\\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);
Resolver = require('../');

const cwd = '/temp/project';
const src = '/root/path/to/node_modules';
const resolver = new Resolver(moduleMap, {
moduleDirectories: [src, 'node_modules'],
});
const dirs_expected = [
src,
cwd + '/node_modules',
path.posix.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;
}