From 6cc323d259fe7cb58825a7e9af8589d63739508d Mon Sep 17 00:00:00 2001 From: Giancarlo Anemone Date: Tue, 28 Apr 2020 13:24:21 -0400 Subject: [PATCH] Support both environment variable and --preserve-symlinks node flag --- .../moduleNameMapper.test.ts.snap | 4 ++-- .../resolveNoFileExtensions.test.ts.snap | 2 +- e2e/__tests__/preserveSymlinks.ts | 23 ++++++++++++++++++- e2e/runJest.ts | 5 +++- packages/jest-cli/package.json | 3 ++- packages/jest-cli/should-preserve-links.d.ts | 10 ++++++++ packages/jest-cli/src/cli/index.ts | 5 ++-- packages/jest-cli/src/init/index.ts | 5 ++-- packages/jest-config/package.json | 3 ++- .../jest-config/should-preserve-links.d.ts | 10 ++++++++ packages/jest-config/src/getCacheDirectory.ts | 6 +++-- packages/jest-config/src/index.ts | 5 ++-- packages/jest-config/src/normalize.ts | 5 ++-- packages/jest-core/package.json | 3 ++- packages/jest-core/should-preserve-links.d.ts | 10 ++++++++ packages/jest-core/src/runJest.ts | 5 ++-- packages/jest-haste-map/package.json | 3 ++- .../jest-haste-map/should-preserve-links.d.ts | 10 ++++++++ packages/jest-haste-map/src/index.ts | 6 +++-- packages/jest-resolve/package.json | 3 ++- .../jest-resolve/should-preserve-links.d.ts | 10 ++++++++ packages/jest-resolve/src/defaultResolver.ts | 8 ++++--- packages/jest-resolve/src/index.ts | 5 ++-- packages/jest-resolve/src/nodeModulesPaths.ts | 6 +++-- packages/jest-runtime/package.json | 3 ++- .../jest-runtime/should-preserve-links.d.ts | 10 ++++++++ packages/jest-runtime/src/cli/index.ts | 5 ++-- packages/jest-transform/package.json | 3 ++- .../jest-transform/should-preserve-links.d.ts | 10 ++++++++ .../jest-transform/src/ScriptTransformer.ts | 5 ++-- yarn.lock | 5 ++++ 31 files changed, 159 insertions(+), 37 deletions(-) create mode 100644 packages/jest-cli/should-preserve-links.d.ts create mode 100644 packages/jest-config/should-preserve-links.d.ts create mode 100644 packages/jest-core/should-preserve-links.d.ts create mode 100644 packages/jest-haste-map/should-preserve-links.d.ts create mode 100644 packages/jest-resolve/should-preserve-links.d.ts create mode 100644 packages/jest-runtime/should-preserve-links.d.ts create mode 100644 packages/jest-transform/should-preserve-links.d.ts diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index ab9cc8259dc3..d65fa16fcb5d 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:551:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:561: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:551:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:561: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 682ff0936cdc..2eb8081795f7 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:305:11) + at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:315:11) at Object.require (index.js:8:18) `; diff --git a/e2e/__tests__/preserveSymlinks.ts b/e2e/__tests__/preserveSymlinks.ts index a86227a87f7a..df9a2505d873 100644 --- a/e2e/__tests__/preserveSymlinks.ts +++ b/e2e/__tests__/preserveSymlinks.ts @@ -53,7 +53,7 @@ afterAll(() => { cleanup(); }); -test('preserving symlinks', () => { +test('preserving symlinks with environment variable', () => { const {stderr, exitCode} = runJest('preserve-symlinks', ['--no-watchman'], { preserveSymlinks: '1', }); @@ -68,6 +68,21 @@ test('preserving symlinks', () => { expect(summary).toMatch('Snapshots: 0 total'); }); +test('preserving symlinks with --preserve-symlinks node flag', () => { + const {stderr, exitCode} = runJest('preserve-symlinks', ['--no-watchman'], { + nodeFlags: ['--preserve-symlinks'], + }); + const {summary, rest} = extractSummary(stderr); + expect(exitCode).toEqual(0); + expect(rest.split('\n').length).toEqual(3); + expect(rest).toMatch('PASS __tests__/ab.test.js'); + expect(rest).toMatch('PASS __tests__/a.test.js'); + expect(rest).toMatch('PASS __tests__/b.test.js'); + expect(summary).toMatch('Test Suites: 3 passed, 3 total'); + expect(summary).toMatch('Tests: 3 passed, 3 total'); + expect(summary).toMatch('Snapshots: 0 total'); +}); + test('hasteMap finds symlinks correctly', async () => { const options = { extensions: ['js'], @@ -102,3 +117,9 @@ test('hasteMap finds symlinks correctly', async () => { expect(files).toContain(f); }); }); + +test('no preserve symlinks configuration', () => { + const {exitCode, stdout} = runJest('preserve-symlinks', ['--no-watchman']); + expect(exitCode).toEqual(1); + expect(stdout).toMatch('No tests found, exiting with code 1'); +}); diff --git a/e2e/runJest.ts b/e2e/runJest.ts index 2517a0066417..c538ae56c6e7 100644 --- a/e2e/runJest.ts +++ b/e2e/runJest.ts @@ -19,6 +19,7 @@ const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js'); type RunJestOptions = { preserveSymlinks?: string; + nodeFlags?: Array; nodeOptions?: string; nodePath?: string; skipPkgJsonCheck?: boolean; // don't complain if can't find package.json @@ -75,13 +76,15 @@ function spawnJest( ); } const env = Object.assign({}, process.env, {FORCE_COLOR: '0'}); - if (options.nodeOptions) env['NODE_OPTIONS'] = options.nodeOptions; if (options.nodePath) env['NODE_PATH'] = options.nodePath; if (options.preserveSymlinks) env['NODE_PRESERVE_SYMLINKS'] = options.preserveSymlinks; const spawnArgs = [JEST_PATH, ...args]; + if (options.nodeFlags) { + spawnArgs.unshift(...options.nodeFlags); + } const spawnOptions = { cwd: dir, env, diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index f556af3d0144..9d93002eff93 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -12,6 +12,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@jest/core": "^25.4.0", "@jest/test-result": "^25.4.0", "@jest/types": "^25.4.0", @@ -79,4 +80,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-cli/should-preserve-links.d.ts b/packages/jest-cli/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-cli/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-cli/src/cli/index.ts b/packages/jest-cli/src/cli/index.ts index cd8489a21f82..6234676fd559 100644 --- a/packages/jest-cli/src/cli/index.ts +++ b/packages/jest-cli/src/cli/index.ts @@ -16,12 +16,13 @@ import chalk = require('chalk'); import exit = require('exit'); import yargs = require('yargs'); import {sync as _realpath} from 'realpath-native'; +import shouldPreserveSymlinks from 'should-preserve-links'; import init from '../init'; import * as args from './args'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } export async function run( diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index 64bdce6ffff5..da1448f2c178 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -11,6 +11,7 @@ import chalk = require('chalk'); import prompts = require('prompts'); import {sync as _realpath} from 'realpath-native'; import {constants} from 'jest-config'; +import shouldPreserveSymlinks from 'should-preserve-links'; import defaultQuestions, {testScriptQuestion} from './questions'; import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors'; import generateConfigFile from './generate_config_file'; @@ -32,9 +33,9 @@ type PromptsResults = { scripts: boolean; }; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 7cf920caf17f..50ebc1997b14 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -17,6 +17,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@babel/core": "^7.1.0", "@jest/test-sequencer": "^25.4.0", "@jest/types": "^25.4.0", @@ -48,4 +49,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-config/should-preserve-links.d.ts b/packages/jest-config/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-config/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-config/src/getCacheDirectory.ts b/packages/jest-config/src/getCacheDirectory.ts index f77c9a37b786..c42909c8b7f2 100644 --- a/packages/jest-config/src/getCacheDirectory.ts +++ b/packages/jest-config/src/getCacheDirectory.ts @@ -9,9 +9,11 @@ import * as path from 'path'; import {tmpdir} from 'os'; import {sync as _realpath} from 'realpath-native'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +import shouldPreserveSymlinks from 'should-preserve-links'; + +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } const getCacheDirectory = () => { diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index 9c7f758b7006..0bae14f71a24 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -10,6 +10,7 @@ import * as path from 'path'; import type {Config} from '@jest/types'; import chalk = require('chalk'); import {sync as _realpath} from 'realpath-native'; +import shouldPreserveSymlinks from 'should-preserve-links'; import {isJSONString, replaceRootDirInPath} from './utils'; import normalize from './normalize'; import resolveConfigPath from './resolveConfigPath'; @@ -23,9 +24,9 @@ export {default as descriptions} from './Descriptions'; import * as constants from './constants'; export {constants}; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } type ReadConfig = { diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 6fd19942784d..e546f4d0cdb1 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -18,6 +18,7 @@ import {sync as _realpath} from 'realpath-native'; import Resolver = require('jest-resolve'); import {replacePathSepForRegex} from 'jest-regex-util'; import merge = require('deepmerge'); +import shouldPreserveSymlinks from 'should-preserve-links'; import validatePattern from './validatePattern'; import getMaxWorkers from './getMaxWorkers'; import { @@ -45,9 +46,9 @@ const PRESET_NAME = 'jest-preset'; type AllOptions = Config.ProjectConfig & Config.GlobalConfig; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } const createConfigError = (message: string) => diff --git a/packages/jest-core/package.json b/packages/jest-core/package.json index 19a6c5d804b8..c852a171c884 100644 --- a/packages/jest-core/package.json +++ b/packages/jest-core/package.json @@ -12,6 +12,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@jest/console": "^25.4.0", "@jest/reporters": "^25.4.0", "@jest/test-result": "^25.4.0", @@ -93,4 +94,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-core/should-preserve-links.d.ts b/packages/jest-core/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-core/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 701abfab4eda..676142907785 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -23,6 +23,7 @@ import { } from '@jest/test-result'; import type TestSequencer from '@jest/test-sequencer'; import type {ChangedFiles, ChangedFilesPromise} from 'jest-changed-files'; +import shouldPreserveSymlinks from 'should-preserve-links'; import getNoTestsFoundMessage from './getNoTestsFoundMessage'; import runGlobalHook from './runGlobalHook'; import SearchSource from './SearchSource'; @@ -32,9 +33,9 @@ import collectNodeHandles from './collectHandles'; import type TestWatcher from './TestWatcher'; import type {Filter, TestRunData} from './types'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } const getTestPaths = async ( diff --git a/packages/jest-haste-map/package.json b/packages/jest-haste-map/package.json index 8d8db4832b20..9d1d9f553d40 100644 --- a/packages/jest-haste-map/package.json +++ b/packages/jest-haste-map/package.json @@ -17,6 +17,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@jest/types": "^25.4.0", "anymatch": "^3.0.3", "fb-watchman": "^2.0.0", @@ -49,4 +50,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-haste-map/should-preserve-links.d.ts b/packages/jest-haste-map/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-haste-map/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index f6451545d0e7..f267f5cc0220 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -15,6 +15,7 @@ import {NodeWatcher, Watcher as SaneWatcher} from 'sane'; import type {Config} from '@jest/types'; import serializer from 'jest-serializer'; import Worker from 'jest-worker'; +import shouldPreserveSymlinks from 'should-preserve-links'; import {getSha1, worker} from './worker'; import getMockName from './getMockName'; import getPlatformExtension from './lib/getPlatformExtension'; @@ -45,6 +46,8 @@ import type { WorkerMetadata, } from './types'; +const preserveSymlinks = shouldPreserveSymlinks(); + type HType = typeof H; type Options = { @@ -269,8 +272,7 @@ class HasteMap extends EventEmitter { : null, name: options.name, platforms: options.platforms, - preserveSymlinks: - options.preserveSymlinks || Boolean(process.env.NODE_PRESERVE_SYMLINKS), + preserveSymlinks: options.preserveSymlinks || preserveSymlinks, resetCache: options.resetCache, retainAllFiles: options.retainAllFiles, rootDir: options.rootDir, diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index 7dc336af75c3..668890880cd2 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -17,6 +17,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@jest/types": "^25.4.0", "browser-resolve": "^1.11.3", "chalk": "^3.0.0", @@ -38,4 +39,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-resolve/should-preserve-links.d.ts b/packages/jest-resolve/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-resolve/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index b6f5a75da0e9..1490c6d4fa00 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -12,9 +12,11 @@ import {sync as _realpath} from 'realpath-native'; import pnpResolver from 'jest-pnp-resolver'; import type {Config} from '@jest/types'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +import shouldPreserveSymlinks from 'should-preserve-links'; + +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } type ResolverOptions = { @@ -45,7 +47,7 @@ export default function defaultResolver( isFile, moduleDirectory: options.moduleDirectory, paths: options.paths, - preserveSymlinks: Boolean(PRESERVE_SYMLINKS), + preserveSymlinks, // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/44137 realpathSync, }); diff --git a/packages/jest-resolve/src/index.ts b/packages/jest-resolve/src/index.ts index 9a90ba61f8d3..14dedecfa7ec 100644 --- a/packages/jest-resolve/src/index.ts +++ b/packages/jest-resolve/src/index.ts @@ -10,6 +10,7 @@ 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 shouldPreserveSymlinks from 'should-preserve-links'; import nodeModulesPaths from './nodeModulesPaths'; import isBuiltinModule from './isBuiltinModule'; import defaultResolver, {clearDefaultResolverCache} from './defaultResolver'; @@ -17,9 +18,9 @@ import type {ResolverConfig} from './types'; import ModuleNotFoundError from './ModuleNotFoundError'; import shouldLoadAsEsm, {clearCachedLookups} from './shouldLoadAsEsm'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } type FindNodeModuleConfig = { diff --git a/packages/jest-resolve/src/nodeModulesPaths.ts b/packages/jest-resolve/src/nodeModulesPaths.ts index b6d3f68631c8..542a03061f66 100644 --- a/packages/jest-resolve/src/nodeModulesPaths.ts +++ b/packages/jest-resolve/src/nodeModulesPaths.ts @@ -16,9 +16,11 @@ type NodeModulesPathsOptions = { paths?: Array; }; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +import shouldPreserveSymlinks from 'should-preserve-links'; + +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } export default function nodeModulesPaths( diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index 856194f2d8fc..756fe236e709 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -17,6 +17,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@jest/console": "^25.4.0", "@jest/environment": "^25.4.0", "@jest/globals": "^25.4.0", @@ -61,4 +62,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-runtime/should-preserve-links.d.ts b/packages/jest-runtime/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-runtime/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index 10467523cc52..4dfbe85713af 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -16,13 +16,14 @@ import {CustomConsole} from '@jest/console'; import {setGlobal} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; import {deprecationEntries, readConfig} from 'jest-config'; +import shouldPreserveSymlinks from 'should-preserve-links'; import {VERSION} from '../version'; import type {Context} from '../types'; import * as args from './args'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } export async function run( diff --git a/packages/jest-transform/package.json b/packages/jest-transform/package.json index d3e250138097..3b5b51b14242 100644 --- a/packages/jest-transform/package.json +++ b/packages/jest-transform/package.json @@ -17,6 +17,7 @@ } }, "dependencies": { + "should-preserve-links": "^1.0.0", "@babel/core": "^7.1.0", "@jest/types": "^25.4.0", "babel-plugin-istanbul": "^6.0.0", @@ -51,4 +52,4 @@ "access": "public" }, "gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638" -} +} \ No newline at end of file diff --git a/packages/jest-transform/should-preserve-links.d.ts b/packages/jest-transform/should-preserve-links.d.ts new file mode 100644 index 000000000000..7fa8fb90fd2b --- /dev/null +++ b/packages/jest-transform/should-preserve-links.d.ts @@ -0,0 +1,10 @@ +/** + * 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. + */ + +declare module 'should-preserve-links' { + export default function shouldPreserveLinks(): boolean; +} diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 721796b5b6e2..cfb382296b86 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -20,6 +20,7 @@ import slash = require('slash'); import {sync as writeFileAtomic} from 'write-file-atomic'; import {sync as _realpath} from 'realpath-native'; import {addHook} from 'pirates'; +import shouldPreserveSymlinks from 'should-preserve-links'; import type { Options, TransformResult, @@ -29,9 +30,9 @@ import type { import shouldInstrument from './shouldInstrument'; import handlePotentialSyntaxError from './enhanceUnexpectedTokenMessage'; -const PRESERVE_SYMLINKS = process.env.NODE_PRESERVE_SYMLINKS; +const preserveSymlinks = shouldPreserveSymlinks(); function realpath(p: string) { - return PRESERVE_SYMLINKS ? p : _realpath(p); + return preserveSymlinks ? p : _realpath(p); } type ProjectCache = { diff --git a/yarn.lock b/yarn.lock index 9ca11d8bef55..e4b433fd9733 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13165,6 +13165,11 @@ shellwords@^0.1.1: resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww== +should-preserve-links@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/should-preserve-links/-/should-preserve-links-1.0.0.tgz#4481b94050d169a1adf58abb7e5b209a978fe332" + integrity sha512-SSq2Qupb+bjpY0bNBgi55U6Wj55oqtz1BxJTj+eargQmVkaVCcFqYaQRoAhwiys0oHCZRDYRVQVL+TWB9Zr6DA== + side-channel@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.2.tgz#df5d1abadb4e4bf4af1cd8852bf132d2f7876947"