From 75b2fbe66684538ba0a2ea7323f48dcce77448fe Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 26 Jan 2019 16:33:48 +0100 Subject: [PATCH] fix: break dependency cycle in jest-cli (#7707) --- CHANGELOG.md | 2 ++ e2e/__tests__/runProgramatically.test.js | 19 +++++++++++++++++++ e2e/run-programatically/index.js | 13 +++++++++++++ packages/jest-cli/src/cli/index.js | 3 +-- packages/jest-cli/src/jest.js | 5 ++--- packages/jest-cli/src/version.js | 14 ++++++++++++++ 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 e2e/__tests__/runProgramatically.test.js create mode 100644 e2e/run-programatically/index.js create mode 100644 packages/jest-cli/src/version.js diff --git a/CHANGELOG.md b/CHANGELOG.md index bfe5dd6e1e43..957ba423d82c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-cli]` Break dependency cycle when using Jest programmatically ([#7707](https://github.com/facebook/jest/pull/7707) + ### Chore & Maintenance - `[website]` Fix broken help link on homepage ([#7706](https://github.com/facebook/jest/pull/7706)) diff --git a/e2e/__tests__/runProgramatically.test.js b/e2e/__tests__/runProgramatically.test.js new file mode 100644 index 000000000000..a9935672f962 --- /dev/null +++ b/e2e/__tests__/runProgramatically.test.js @@ -0,0 +1,19 @@ +/** + * 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. + * + * @flow + */ + +import {resolve} from 'path'; + +import {run} from '../Utils'; + +const dir = resolve(__dirname, '..', 'run-programatically'); + +test('run Jest programatically', () => { + const {stdout} = run(`node index.js --version`, dir); + expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[\-\S]*-dev$/); +}); diff --git a/e2e/run-programatically/index.js b/e2e/run-programatically/index.js new file mode 100644 index 000000000000..a397521b7c74 --- /dev/null +++ b/e2e/run-programatically/index.js @@ -0,0 +1,13 @@ +#!/usr/bin/env node +/** + * 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. + * + * @flow + */ + +// Running Jest like this is not officially supported, +// but it is common practice until there is a proper API as a substitute. +require('jest').run(process.argv); diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 031c92d53446..a5d766fdd490 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -33,8 +33,7 @@ import rimraf from 'rimraf'; import {sync as realpath} from 'realpath-native'; import init from '../lib/init'; import logDebugMessages from '../lib/log_debug_messages'; - -const {getVersion} = require('../jest'); +import getVersion from '../version'; export async function run(maybeArgv?: Argv, project?: Path) { try { diff --git a/packages/jest-cli/src/jest.js b/packages/jest-cli/src/jest.js index 98011013820f..22210bef096e 100644 --- a/packages/jest-cli/src/jest.js +++ b/packages/jest-cli/src/jest.js @@ -7,18 +7,17 @@ * @flow */ -import {version as VERSION} from '../package.json'; - import SearchSource from './SearchSource'; import TestScheduler from './TestScheduler'; import TestWatcher from './TestWatcher'; import {run, runCLI} from './cli'; +import getVersion from './version'; module.exports = { SearchSource, TestScheduler, TestWatcher, - getVersion: () => VERSION, + getVersion, run, runCLI, }; diff --git a/packages/jest-cli/src/version.js b/packages/jest-cli/src/version.js new file mode 100644 index 000000000000..23a05d19add1 --- /dev/null +++ b/packages/jest-cli/src/version.js @@ -0,0 +1,14 @@ +/** + * 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. + * + * @flow + */ + +import {version as VERSION} from '../package.json'; + +export default function getVersion() { + return VERSION; +}