Skip to content

Commit

Permalink
fix jestjs#5396: preserve absolute paths in moduleDirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Jan 28, 2018
1 parent 0110101 commit c2b74a7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
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;
}

0 comments on commit c2b74a7

Please sign in to comment.