diff --git a/CHANGELOG.md b/CHANGELOG.md index a8175d2a73b6..020c97b4cb4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[*]` [**BREAKING**] TypeScript definitions requires a minimum of TypeScript v3.8 ([#9823](https://github.com/facebook/jest/pull/9823)) - `[*]` [**BREAKING**] Drop support for Node 8 ([#9423](https://github.com/facebook/jest/pull/9423)) - `[*]` Upgrade to chalk@4 ([#9752](https://github.com/facebook/jest/pull/9752)) +- `[*]` Remove usage of `realpath-native` ([#9952](https://github.com/facebook/jest/pull/9952)) - `[jest-runtime]` [**BREAKING**] Remove long-deprecated `require.requireActual` and `require.requireMock` methods ([#9854](https://github.com/facebook/jest/pull/9854)) - `[expect, jest-mock, pretty-format]` [**BREAKING**] Remove `build-es5` from package ([#9945](https://github.com/facebook/jest/pull/9945)) - `[jest-haste-map]` [**BREAKING**] removed `providesModuleNodeModules` ([#8535](https://github.com/facebook/jest/pull/8535)) diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index 3541cc32ec0a..d65a0391ff07 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -36,7 +36,7 @@ FAIL __tests__/index.js 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:542:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:541:17) at Object.require (index.js:10:1) `; @@ -65,6 +65,6 @@ FAIL __tests__/index.js 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:542:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:541:17) at Object.require (index.js:10:1) `; diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap index 3796725b10a7..a62a66e69f80 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap @@ -37,6 +37,6 @@ FAIL __tests__/test.js | ^ 9 | - at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:297:11) + at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:296:11) at Object.require (index.js:8:18) `; diff --git a/e2e/__tests__/hasteMapSize.test.ts b/e2e/__tests__/hasteMapSize.test.ts index 610ef1af3227..228eb83f1e7d 100644 --- a/e2e/__tests__/hasteMapSize.test.ts +++ b/e2e/__tests__/hasteMapSize.test.ts @@ -7,11 +7,11 @@ import {tmpdir} from 'os'; import * as path from 'path'; +import {realpathSync} from 'graceful-fs'; import HasteMap = require('jest-haste-map'); -import {sync as realpath} from 'realpath-native'; import {cleanup, writeFiles} from '../Utils'; -const DIR = path.resolve(realpath(tmpdir()), 'haste_map_size'); +const DIR = path.resolve(realpathSync.native(tmpdir()), 'haste_map_size'); beforeEach(() => { cleanup(DIR); diff --git a/package.json b/package.json index 2ef156fc4462..d878cd137cf5 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,6 @@ "prettier": "^2.0.1", "progress": "^2.0.0", "promise": "^8.0.2", - "realpath-native": "^2.0.0", "resolve": "^1.15.0", "rimraf": "^3.0.0", "semver": "^6.3.0", diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 2cdeae5b3054..eb3bfc715299 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -17,7 +17,6 @@ "jest-util": "^26.0.0-alpha.0", "jest-validate": "^26.0.0-alpha.0", "prompts": "^2.0.1", - "realpath-native": "^2.0.0", "yargs": "^15.3.1" }, "devDependencies": { diff --git a/packages/jest-cli/src/cli/index.ts b/packages/jest-cli/src/cli/index.ts index 06fb3bf4d31a..41d3302c7e0c 100644 --- a/packages/jest-cli/src/cli/index.ts +++ b/packages/jest-cli/src/cli/index.ts @@ -8,14 +8,13 @@ import * as path from 'path'; import type {Config} from '@jest/types'; import type {AggregatedResult} from '@jest/test-result'; -import {clearLine} from 'jest-util'; +import {clearLine, tryRealpath} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; import {deprecationEntries} from 'jest-config'; import {getVersion, runCLI} from '@jest/core'; import chalk = require('chalk'); import exit = require('exit'); import yargs = require('yargs'); -import {sync as realpath} from 'realpath-native'; import init from '../init'; import * as args from './args'; @@ -97,7 +96,7 @@ const getProjectListFromCLIArgs = ( if (!projects.length && process.platform === 'win32') { try { - projects.push(realpath(process.cwd())); + projects.push(tryRealpath(process.cwd())); } catch (err) { // do nothing, just catch error // process.binding('fs').realpath can throw, e.g. on mapped drives diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index b2503fc901f3..bd7c030b09e3 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -9,8 +9,8 @@ import * as path from 'path'; import * as fs from 'graceful-fs'; import chalk = require('chalk'); import prompts = require('prompts'); -import {sync as realpath} from 'realpath-native'; import {constants} from 'jest-config'; +import {tryRealpath} from 'jest-util'; import defaultQuestions, {testScriptQuestion} from './questions'; import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors'; import generateConfigFile from './generate_config_file'; @@ -35,7 +35,7 @@ type PromptsResults = { const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; export default async ( - rootDir: string = realpath(process.cwd()), + rootDir: string = tryRealpath(process.cwd()), ): Promise => { // prerequisite checks const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index baba47cccfc1..a5f29e8168d2 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -27,8 +27,7 @@ "jest-util": "^26.0.0-alpha.0", "jest-validate": "^26.0.0-alpha.0", "micromatch": "^4.0.2", - "pretty-format": "^26.0.0-alpha.0", - "realpath-native": "^2.0.0" + "pretty-format": "^26.0.0-alpha.0" }, "devDependencies": { "@types/babel__core": "^7.0.4", diff --git a/packages/jest-config/src/getCacheDirectory.ts b/packages/jest-config/src/getCacheDirectory.ts index 2871d1e5eaab..c23a08870a26 100644 --- a/packages/jest-config/src/getCacheDirectory.ts +++ b/packages/jest-config/src/getCacheDirectory.ts @@ -7,11 +7,12 @@ import * as path from 'path'; import {tmpdir} from 'os'; -import {sync as realpath} from 'realpath-native'; +import type {Config} from '@jest/types'; +import {tryRealpath} from 'jest-util'; -const getCacheDirectory = () => { +const getCacheDirectory: () => Config.Path = () => { const {getuid} = process; - const tmpdirPath = path.join(realpath(tmpdir()), 'jest'); + const tmpdirPath = path.join(tryRealpath(tmpdir()), 'jest'); if (getuid == null) { return tmpdirPath; } else { diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 0bcb65fc670e..68d6b5ddf55c 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -8,8 +8,8 @@ import * as path from 'path'; import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; +import {tryRealpath} from 'jest-util'; import chalk = require('chalk'); -import {sync as realpath} from 'realpath-native'; import {isJSONString, replaceRootDirInPath} from './utils'; import normalize from './normalize'; import resolveConfigPath from './resolveConfigPath'; @@ -295,7 +295,7 @@ export async function readConfigs( if (projects.length > 0) { const projectIsCwd = process.platform === 'win32' - ? projects[0] === realpath(process.cwd()) + ? projects[0] === tryRealpath(process.cwd()) : projects[0] === process.cwd(); const parsedConfigs = await Promise.all( diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 75727fa23c74..8ec7da7f35a2 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -11,10 +11,9 @@ import {statSync} from 'graceful-fs'; import {sync as glob} from 'glob'; import type {Config} from '@jest/types'; import {ValidationError, validate} from 'jest-validate'; -import {clearLine, replacePathSepForGlob} from 'jest-util'; +import {clearLine, replacePathSepForGlob, tryRealpath} from 'jest-util'; import chalk = require('chalk'); import micromatch = require('micromatch'); -import {sync as realpath} from 'realpath-native'; import Resolver = require('jest-resolve'); import {replacePathSepForRegex} from 'jest-regex-util'; import merge = require('deepmerge'); @@ -391,7 +390,7 @@ const normalizeRootDir = ( try { // try to resolve windows short paths, ignoring errors (permission errors, mostly) - options.rootDir = realpath(options.rootDir); + options.rootDir = tryRealpath(options.rootDir); } catch (e) { // ignored } @@ -955,7 +954,7 @@ export default function normalize( try { // try to resolve windows short paths, ignoring errors (permission errors, mostly) - newOptions.cwd = realpath(process.cwd()); + newOptions.cwd = tryRealpath(process.cwd()); } catch (e) { // ignored } diff --git a/packages/jest-core/package.json b/packages/jest-core/package.json index 98d95d21bcdf..831e401092e6 100644 --- a/packages/jest-core/package.json +++ b/packages/jest-core/package.json @@ -29,7 +29,6 @@ "jest-watcher": "^26.0.0-alpha.0", "micromatch": "^4.0.2", "p-each-series": "^2.1.0", - "realpath-native": "^2.0.0", "rimraf": "^3.0.0", "slash": "^3.0.0", "strip-ansi": "^6.0.0" diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index bea930899c0d..91107ee0637c 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -7,9 +7,8 @@ import * as path from 'path'; import chalk = require('chalk'); -import {sync as realpath} from 'realpath-native'; import {CustomConsole} from '@jest/console'; -import {interopRequireDefault} from 'jest-util'; +import {interopRequireDefault, tryRealpath} from 'jest-util'; import exit = require('exit'); import * as fs from 'graceful-fs'; import {JestHook, JestHookEmitter} from 'jest-watcher'; @@ -100,7 +99,7 @@ const processResults = ( } if (isJSON) { if (outputFile) { - const cwd = realpath(process.cwd()); + const cwd = tryRealpath(process.cwd()); const filePath = path.resolve(cwd, outputFile); fs.writeFileSync(filePath, JSON.stringify(formatTestResults(runResults))); diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index f563a3946c0a..91936f09cf0f 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -11,11 +11,11 @@ "types": "build/index.d.ts", "dependencies": { "@jest/types": "^26.0.0-alpha.0", + "jest-util": "^26.0.0-alpha.0", "chalk": "^4.0.0", "graceful-fs": "^4.2.4", "jest-pnp-resolver": "^1.2.1", "read-pkg-up": "^7.0.1", - "realpath-native": "^2.0.0", "resolve": "^1.17.0", "slash": "^3.0.0" }, diff --git a/packages/jest-resolve/src/__tests__/resolve.test.ts b/packages/jest-resolve/src/__tests__/resolve.test.ts index a1f7f49a865b..6ecefafd513d 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.ts +++ b/packages/jest-resolve/src/__tests__/resolve.test.ts @@ -234,8 +234,11 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => { // pathstrings instead of actually trying to access the physical directory. // This test suite won't work otherwise, since we cannot make assumptions // about the test environment when it comes to absolute paths. - jest.doMock('realpath-native', () => ({ - sync: (dirInput: string) => dirInput, + jest.doMock('graceful-fs', () => ({ + ...jest.requireActual('graceful-fs'), + realPathSync: { + native: (dirInput: string) => dirInput, + }, })); }); diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index cb570d390a5a..77e4c6981546 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -7,8 +7,8 @@ import * as fs from 'graceful-fs'; import {sync as resolveSync} from 'resolve'; -import {sync as realpath} from 'realpath-native'; import pnpResolver from 'jest-pnp-resolver'; +import {tryRealpath} from 'jest-util'; import type {Config} from '@jest/types'; type ResolverOptions = { @@ -95,17 +95,7 @@ function realpathCached(path: Config.Path): Config.Path { return result; } - try { - result = realpath(path); - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } - } - - if (!result) { - result = path; - } + result = tryRealpath(path); checkedRealpathPaths.set(path, result); diff --git a/packages/jest-resolve/src/index.ts b/packages/jest-resolve/src/index.ts index 1c04a9429f96..1efebd10b974 100644 --- a/packages/jest-resolve/src/index.ts +++ b/packages/jest-resolve/src/index.ts @@ -8,14 +8,14 @@ import * as path from 'path'; import type {Config} from '@jest/types'; import type {ModuleMap} from 'jest-haste-map'; -import {sync as realpath} from 'realpath-native'; -import chalk = require('chalk'); +import {tryRealpath} from 'jest-util'; import nodeModulesPaths from './nodeModulesPaths'; import isBuiltinModule from './isBuiltinModule'; import defaultResolver, {clearDefaultResolverCache} from './defaultResolver'; import type {ResolverConfig} from './types'; import ModuleNotFoundError from './ModuleNotFoundError'; import shouldLoadAsEsm, {clearCachedLookups} from './shouldLoadAsEsm'; +import chalk = require('chalk'); type FindNodeModuleConfig = { basedir: Config.Path; @@ -41,8 +41,7 @@ namespace Resolver { const NATIVE_PLATFORM = 'native'; // We might be inside a symlink. -const cwd = process.cwd(); -const resolvedCwd = realpath(cwd) || cwd; +const resolvedCwd = tryRealpath(process.cwd()); const {NODE_PATH} = process.env; const nodePaths = NODE_PATH ? NODE_PATH.split(path.delimiter) diff --git a/packages/jest-resolve/src/nodeModulesPaths.ts b/packages/jest-resolve/src/nodeModulesPaths.ts index dddf52229a13..1c311b70df52 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.ts +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -9,7 +9,7 @@ import * as path from 'path'; import type {Config} from '@jest/types'; -import {sync as realpath} from 'realpath-native'; +import {tryRealpath} from 'jest-util'; type NodeModulesPathsOptions = { moduleDirectory?: Array; @@ -40,7 +40,7 @@ export default function nodeModulesPaths( // traverses parents of the physical path, not the symlinked path let physicalBasedir; try { - physicalBasedir = realpath(basedirAbs); + physicalBasedir = tryRealpath(basedirAbs); } catch (err) { // realpath can throw, e.g. on mapped drives physicalBasedir = basedirAbs; diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index 15664857cc1b..2c55d75c6318 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -32,7 +32,6 @@ "jest-snapshot": "^26.0.0-alpha.0", "jest-util": "^26.0.0-alpha.0", "jest-validate": "^26.0.0-alpha.0", - "realpath-native": "^2.0.0", "slash": "^3.0.0", "strip-bom": "^4.0.0", "yargs": "^15.3.1" diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index 099373e2f67c..490848a2ba1e 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -8,12 +8,11 @@ import {cpus} from 'os'; import * as path from 'path'; import chalk = require('chalk'); -import {sync as realpath} from 'realpath-native'; import yargs = require('yargs'); import type {Config} from '@jest/types'; import type {JestEnvironment} from '@jest/environment'; import {CustomConsole} from '@jest/console'; -import {setGlobal} from 'jest-util'; +import {setGlobal, tryRealpath} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; import {deprecationEntries, readConfig} from 'jest-config'; import {VERSION} from '../version'; @@ -53,7 +52,7 @@ export async function run( return; } - const root = realpath(process.cwd()); + const root = tryRealpath(process.cwd()); const filePath = path.resolve(root, argv._[0]); if (argv.debug) { diff --git a/packages/jest-transform/package.json b/packages/jest-transform/package.json index 3265e7083aa3..68e6d8912662 100644 --- a/packages/jest-transform/package.json +++ b/packages/jest-transform/package.json @@ -22,7 +22,6 @@ "jest-util": "^26.0.0-alpha.0", "micromatch": "^4.0.2", "pirates": "^4.0.1", - "realpath-native": "^2.0.0", "slash": "^3.0.0", "source-map": "^0.6.1", "write-file-atomic": "^3.0.0" diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 46ae3afb049f..0cf17915da3c 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -8,7 +8,12 @@ import {createHash} from 'crypto'; import * as path from 'path'; import type {Config} from '@jest/types'; -import {createDirectory, interopRequireDefault, isPromise} from 'jest-util'; +import { + createDirectory, + interopRequireDefault, + isPromise, + tryRealpath, +} from 'jest-util'; import * as fs from 'graceful-fs'; import {transformSync as babelTransform} from '@babel/core'; // @ts-ignore: should just be `require.resolve`, but the tests mess that up @@ -18,7 +23,6 @@ import HasteMap = require('jest-haste-map'); import stableStringify = require('fast-json-stable-stringify'); import slash = require('slash'); import {sync as writeFileAtomic} from 'write-file-atomic'; -import {sync as realpath} from 'realpath-native'; import {addHook} from 'pirates'; import type { Options, @@ -247,14 +251,6 @@ export default class ScriptTransformer { return input; } - private _getRealPath(filepath: Config.Path): Config.Path { - try { - return realpath(filepath) || filepath; - } catch (err) { - return filepath; - } - } - // We don't want to expose transformers to the outside - this function is just // to warm up `this._transformCache` preloadTransformer(filepath: Config.Path): void { @@ -269,7 +265,7 @@ export default class ScriptTransformer { supportsDynamicImport = false, supportsStaticESM = false, ): TransformResult { - const filename = this._getRealPath(filepath); + const filename = tryRealpath(filepath); const transform = this._getTransformer(filename); const cacheFilePath = this._getFileCachePath( filename, diff --git a/packages/jest-transform/src/__tests__/script_transformer.test.js b/packages/jest-transform/src/__tests__/script_transformer.test.js index 6479bc62e1ce..4d2796689fa6 100644 --- a/packages/jest-transform/src/__tests__/script_transformer.test.js +++ b/packages/jest-transform/src/__tests__/script_transformer.test.js @@ -29,7 +29,12 @@ jest }), }), ) - .mock('graceful-fs') + .mock('graceful-fs', () => ({ + ...jest.requireActual('graceful-fs'), + realPathSync: { + native: dirInput => dirInput, + }, + })) .mock('jest-haste-map', () => ({ getCacheFilePath: (cacheDir, baseDir, version) => cacheDir + baseDir, })) diff --git a/packages/jest-util/src/index.ts b/packages/jest-util/src/index.ts index e8318c550bbf..cfd00f7fd55d 100644 --- a/packages/jest-util/src/index.ts +++ b/packages/jest-util/src/index.ts @@ -21,5 +21,6 @@ export {default as testPathPatternToRegExp} from './testPathPatternToRegExp'; import * as preRunMessage from './preRunMessage'; export {default as pluralize} from './pluralize'; export {default as formatTime} from './formatTime'; +export {default as tryRealpath} from './tryRealpath'; export {preRunMessage, specialChars}; diff --git a/packages/jest-util/src/tryRealpath.ts b/packages/jest-util/src/tryRealpath.ts new file mode 100644 index 000000000000..ff14e377a963 --- /dev/null +++ b/packages/jest-util/src/tryRealpath.ts @@ -0,0 +1,21 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {realpathSync} from 'graceful-fs'; +import type {Config} from '@jest/types'; + +export default function tryRealpath(path: Config.Path): Config.Path { + try { + path = realpathSync.native(path); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + + return path; +}