Skip to content

Commit

Permalink
jest-resolve - only resolve realpath once in try-catch (#6925)
Browse files Browse the repository at this point in the history
Fixes #6880
  • Loading branch information
asapach authored and SimenB committed Aug 30, 2018
1 parent e9350f6 commit 065a0b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-resolve]` Only resolve realpath once in try-catch ([#6925](https://github.com/facebook/jest/pull/6925))
- `[expect]` Fix TypeError in `toBeInstanceOf` on `null` or `undefined` ([#6912](https://github.com/facebook/jest/pull/6912))
- `[jest-jasmine2]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917))
- `[jest-circus]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917))
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-resolve/src/__tests__/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ describe('resolveModule', () => {
const resolver = new Resolver(moduleMap, {
extensions: ['.js'],
});
const src = require.resolve(
'../../src/__mocks__/bar/node_modules/foo/index.js',
const src = path.join(
path.resolve(__dirname, '../../src/__mocks__/bar/node_modules/'),
'foo/index.js',
);
const resolved = resolver.resolveModule(src, 'dep');
expect(resolved).toBe(
Expand Down
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 @@ -38,17 +38,21 @@ export default function nodeModulesPaths(
prefix = '\\\\';
}

// The node resolution algorithm (as implemented by NodeJS
// and TypeScript) traverses parents of the physical path,
// not the symlinked path
const physicalBasedir = realpath(basedirAbs);
// The node resolution algorithm (as implemented by NodeJS and TypeScript)
// traverses parents of the physical path, not the symlinked path
let physicalBasedir;
try {
physicalBasedir = realpath(basedirAbs);
} catch (err) {
// realpath can throw, e.g. on mapped drives
physicalBasedir = basedirAbs;
}

const paths = [physicalBasedir];
let parsed = path.parse(physicalBasedir);
while (parsed.dir !== paths[paths.length - 1]) {
const realParsedDir = realpath(parsed.dir);
paths.push(realParsedDir);
parsed = path.parse(realParsedDir);
paths.push(parsed.dir);
parsed = path.parse(parsed.dir);
}

const dirs = paths
Expand Down

0 comments on commit 065a0b2

Please sign in to comment.