Skip to content

Commit

Permalink
Support both environment variable and --preserve-symlinks node flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Giancarlo Anemone committed Apr 28, 2020
1 parent 3ee56aa commit 6cc323d
Show file tree
Hide file tree
Showing 31 changed files with 159 additions and 37 deletions.
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`;

Expand Down Expand Up @@ -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)
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`;
23 changes: 22 additions & 1 deletion e2e/__tests__/preserveSymlinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand All @@ -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'],
Expand Down Expand Up @@ -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');
});
5 changes: 4 additions & 1 deletion e2e/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');

type RunJestOptions = {
preserveSymlinks?: string;
nodeFlags?: Array<string>;
nodeOptions?: string;
nodePath?: string;
skipPkgJsonCheck?: boolean; // don't complain if can't find package.json
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -79,4 +80,4 @@
"access": "public"
},
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
}
}
10 changes: 10 additions & 0 deletions packages/jest-cli/should-preserve-links.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 3 additions & 2 deletions packages/jest-cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-cli/src/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -48,4 +49,4 @@
"access": "public"
},
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
}
}
10 changes: 10 additions & 0 deletions packages/jest-config/should-preserve-links.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 4 additions & 2 deletions packages/jest-config/src/getCacheDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) =>
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -93,4 +94,4 @@
"access": "public"
},
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
}
}
10 changes: 10 additions & 0 deletions packages/jest-core/should-preserve-links.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 3 additions & 2 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -49,4 +50,4 @@
"access": "public"
},
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
}
}
10 changes: 10 additions & 0 deletions packages/jest-haste-map/should-preserve-links.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 4 additions & 2 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,6 +46,8 @@ import type {
WorkerMetadata,
} from './types';

const preserveSymlinks = shouldPreserveSymlinks();

type HType = typeof H;

type Options = {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -38,4 +39,4 @@
"access": "public"
},
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
}
}
10 changes: 10 additions & 0 deletions packages/jest-resolve/should-preserve-links.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 5 additions & 3 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
});
Expand Down
Loading

0 comments on commit 6cc323d

Please sign in to comment.