diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d9634cf995f..53c3b50f6658 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/jest-resolve/src/__tests__/resolve.test.js b/packages/jest-resolve/src/__tests__/resolve.test.js index acb73929ef9a..05586429d763 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.js +++ b/packages/jest-resolve/src/__tests__/resolve.test.js @@ -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( diff --git a/packages/jest-resolve/src/node_modules_paths.js b/packages/jest-resolve/src/node_modules_paths.js index 88654b0a970e..84cb68a2c3fb 100644 --- a/packages/jest-resolve/src/node_modules_paths.js +++ b/packages/jest-resolve/src/node_modules_paths.js @@ -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