diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eb8e94eea35..bc6d8606eb1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,21 @@ ## master +### Fixes + +* `[jest-cli]` Use `import-local` to support global Jest installations. + ([#5304](https://github.com/facebook/jest/pull/5304)) + ## jest 22.0.7 ### Fixes -* `[jest-runner]` Fix memory leak in coverage reporting ([#5289](https://github.com/facebook/jest/pull/5289)) +* `[jest-runner]` Fix memory leak in coverage reporting + ([#5289](https://github.com/facebook/jest/pull/5289)) ### Features -* `[jest-cli]` Make Jest exit without an error when no tests are found in - the case of `--lastCommit`, `--findRelatedTests`, or `--onlyChanged` options +* `[jest-cli]` Make Jest exit without an error when no tests are found in the + case of `--lastCommit`, `--findRelatedTests`, or `--onlyChanged` options having been passed to the CLI ### Fixes @@ -24,13 +30,15 @@ ### Fixes -* `[jest-jasmine2]` Fix memory leak in snapshot reporting ([#5279](https://github.com/facebook/jest/pull/5279)) -* `[jest-config]` Fix breaking change in `--testPathPattern` ([#5269](https://github.com/facebook/jest/pull/5269)) +* `[jest-jasmine2]` Fix memory leak in snapshot reporting + ([#5279](https://github.com/facebook/jest/pull/5279)) +* `[jest-config]` Fix breaking change in `--testPathPattern` + ([#5269](https://github.com/facebook/jest/pull/5269)) ### Fixes * `[docs]` Document caveat with mocks, Enzyme, snapshots and React 16 - ([#5258](https://github.com/facebook/jest/issues/5258)) + ([#5258](https://github.com/facebook/jest/issues/5258)) ## jest 22.0.5 diff --git a/packages/jest-cli/bin/jest.js b/packages/jest-cli/bin/jest.js index 101e506c9e9f..fb02a852250a 100755 --- a/packages/jest-cli/bin/jest.js +++ b/packages/jest-cli/bin/jest.js @@ -6,8 +6,12 @@ * LICENSE file in the root directory of this source tree. */ -if (process.env.NODE_ENV == null) { - process.env.NODE_ENV = 'test'; -} +const importLocal = require('import-local'); + +if (!importLocal(__filename)) { + if (process.env.NODE_ENV == null) { + process.env.NODE_ENV = 'test'; + } -require('../build/cli').run(); + require('../build/cli').run(); +} diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 1a28c06c4ec5..d2606dbe1fc0 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -8,6 +8,7 @@ "chalk": "^2.0.1", "glob": "^7.1.2", "graceful-fs": "^4.1.11", + "import-local": "^1.0.0", "is-ci": "^1.0.10", "istanbul-api": "^1.1.14", "istanbul-lib-coverage": "^1.1.1", diff --git a/packages/jest-cli/src/cli/get_jest.js b/packages/jest-cli/src/cli/get_jest.js deleted file mode 100644 index 4c97b663c00e..000000000000 --- a/packages/jest-cli/src/cli/get_jest.js +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. 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. - * - * @flow - */ - -import type {Path} from 'types/Config'; - -import path from 'path'; -import chalk from 'chalk'; -import fs from 'graceful-fs'; -// eslint-disable-next-line import/default -import jest from '../jest'; - -export default function getJest(packageRoot: Path) { - const packageJSONPath = path.join(packageRoot, 'package.json'); - const binPath = path.join(packageRoot, 'node_modules/jest-cli'); - if (fs.existsSync(binPath)) { - /* $FlowFixMe */ - return require(binPath); - } else { - // Check if Jest is specified in `package.json` but not installed. - if (fs.existsSync(packageJSONPath)) { - /* $FlowFixMe */ - const packageJSON = require(packageJSONPath); - const dependencies = packageJSON.dependencies; - const devDependencies = packageJSON.devDependencies; - if ( - (dependencies && dependencies['jest-cli']) || - (devDependencies && devDependencies['jest-cli']) - ) { - process.on('exit', () => - console.log( - chalk.red( - 'Please run `npm install` to use the version of Jest intended ' + - 'for this project.', - ), - ), - ); - } - } - return jest; - } -} diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 6c7b3f45eb92..223b6e40665d 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -23,7 +23,6 @@ import * as args from './args'; import chalk from 'chalk'; import createContext from '../lib/create_context'; import getChangedFilesPromise from '../get_changed_files_promise'; -import getJest from './get_jest'; import handleDeprecationWarnings from '../lib/handle_deprecation_warnings'; import logDebugMessages from '../lib/log_debug_messages'; import {print as preRunMessagePrint} from '../pre_run_message'; @@ -39,11 +38,8 @@ export async function run(maybeArgv?: Argv, project?: Path) { try { const argv: Argv = buildArgv(maybeArgv, project); const projects = getProjectListFromCLIArgs(argv, project); - // If we're running a single Jest project, we might want to use another - // version of Jest (the one that is specified in this project's package.json) - const runCLIFn = getRunCLIFn(projects); - const {results, globalConfig} = await runCLIFn(argv, projects); + const {results, globalConfig} = await runCLI(argv, projects); readResultsAndExit(results, globalConfig); } catch (error) { clearLine(process.stderr); @@ -158,9 +154,6 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => { return projects; }; -const getRunCLIFn = (projects: Array) => - projects.length === 1 ? getJest(projects[0]).runCLI : runCLI; - const printDebugInfoAndExitIfNeeded = ( argv, globalConfig, diff --git a/yarn.lock b/yarn.lock index d6fb69f81221..d770ee32711f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4021,6 +4021,13 @@ immutable@^4.0.0-rc.7: version "4.0.0-rc.9" resolved "https://registry.yarnpkg.com/immutable/-/immutable-4.0.0-rc.9.tgz#1e6e0094e649013ec3742d2b5aeeca5eeda4f0bf" +import-local@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/import-local/-/import-local-1.0.0.tgz#5e4ffdc03f4fe6c009c6729beb29631c2f8227bc" + dependencies: + pkg-dir "^2.0.0" + resolve-cwd "^2.0.0" + import-modules@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/import-modules/-/import-modules-1.1.0.tgz#748db79c5cc42bb9701efab424f894e72600e9dc" @@ -6131,6 +6138,12 @@ pkg-dir@^1.0.0: dependencies: find-up "^1.0.0" +pkg-dir@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b" + dependencies: + find-up "^2.1.0" + plist@2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/plist/-/plist-2.0.1.tgz#0a32ca9481b1c364e92e18dc55c876de9d01da8b" @@ -6872,10 +6885,20 @@ requires-port@1.x.x: version "1.0.0" resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" +resolve-cwd@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-2.0.0.tgz#00a9f7387556e27038eae232caa372a6a59b665a" + dependencies: + resolve-from "^3.0.0" + resolve-from@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" +resolve-from@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748" + resolve@1.1.7: version "1.1.7" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"