From 40563b8b8eea5a04b3e3273422e0fcf76688c87f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 24 Jan 2019 18:22:03 +0100 Subject: [PATCH] create jest-core package --- CHANGELOG.md | 2 + jest.config.js | 6 +- packages/jest-cli/package.json | 34 +-- packages/jest-cli/src/cli/index.js | 279 +++--------------- packages/jest-cli/src/index.js | 10 + .../__tests__/__snapshots__/init.test.js.snap | 0 .../modify_package_json.test.js.snap | 0 .../has_jest_config_file/jest.config.js | 0 .../has_jest_config_file/package.json | 0 .../package.json | 0 .../fixtures/no_package_json/index.js | 0 .../fixtures/only_package_json/package.json | 0 .../test_script_configured/package.json | 0 .../typescript_in_dependencies/package.json | 0 .../package.json | 0 .../src/{lib => init}/__tests__/init.test.js | 2 +- .../__tests__/modify_package_json.test.js | 2 +- packages/jest-cli/src/{ => init}/constants.js | 0 .../jest-cli/src/{lib => }/init/errors.js | 0 .../{lib => }/init/generate_config_file.js | 0 packages/jest-cli/src/{lib => }/init/index.js | 2 +- .../src/{lib => }/init/modify_package_json.js | 0 .../jest-cli/src/{lib => }/init/questions.js | 0 packages/jest-cli/src/jest.js | 23 -- packages/jest-core/.npmignore | 3 + packages/jest-core/README.md | 3 + packages/jest-core/package.json | 82 +++++ .../src/FailedTestsCache.js | 0 .../src/ReporterDispatcher.js | 0 .../src/SearchSource.js | 0 .../src/SnapshotInteractiveMode.js | 0 .../src/TestNamePatternPrompt.js | 0 .../src/TestPathPatternPrompt.js | 0 .../src/TestScheduler.js | 0 .../src/TestSequencer.js | 0 .../src/TestWatcher.js | 0 .../src/__tests__/FailedTestsCache.test.js | 0 .../src/__tests__/SearchSource.test.js | 6 +- .../__tests__/SnapshotInteractiveMode.test.js | 0 .../src/__tests__/TestScheduler.test.js | 0 .../__tests__/__fixtures__/watch_plugin.js | 0 .../__tests__/__fixtures__/watch_plugin2.js | 0 .../SnapshotInteractiveMode.test.js.snap | 0 .../__snapshots__/watch.test.js.snap | 0 .../watch_filename_pattern_mode.test.js.snap | 0 .../watch_test_name_pattern_mode.test.js.snap | 0 .../src/__tests__/globals.test.js | 0 .../src/__tests__/run_jest.test.js | 0 .../src/__tests__/testSchedulerHelper.test.js | 0 .../.hiddenFolder/not-really-a-test.txt | 0 .../__testtests__/do-not-match-me.txt | 0 .../__testtests__/not-really-a-test.txt | 0 .../test_root/__testtests__/test.foobar | 0 .../__tests__/test_root/__testtests__/test.js | 0 .../test_root/__testtests__/test.jsx | 0 .../src/__tests__/test_root/module.foobar | 0 .../src/__tests__/test_root/module.jsx | 0 .../src/__tests__/test_root/no_tests.js | 0 .../__testtests__/test.js | 0 .../test_root_with_(parentheses)/module.jsx | 0 .../src/__tests__/test_sequencer.test.js | 0 .../src/__tests__/watch.test.js | 0 .../watch_filename_pattern_mode.test.js | 0 .../watch_test_name_pattern_mode.test.js | 0 .../src/assets/jest_logo.png | Bin packages/jest-core/src/cli/index.js | 211 +++++++++++++ .../src/collectHandles.js | 0 .../src/coverage.template | 0 .../src/getChangedFilesPromise.js | 0 .../src/getNoTestFound.js | 0 .../src/getNoTestFoundFailed.js | 0 .../getNoTestFoundRelatedToChangedFiles.js | 0 .../src/getNoTestFoundVerbose.js | 0 .../src/getNoTestsFoundMessage.js | 0 packages/jest-core/src/jest.js | 13 + .../log_debug_messages.test.js.snap | 0 .../src/lib/__tests__/is_valid_path.test.js | 0 .../lib/__tests__/log_debug_messages.test.js | 0 .../src/lib/active_filters_message.js | 0 .../src/lib/create_context.js | 0 .../src/lib/handle_deprecation_warnings.js | 0 .../src/lib/is_valid_path.js | 0 .../src/lib/log_debug_messages.js | 0 .../src/lib/update_global_config.js | 0 .../src/lib/watch_plugins_helpers.js | 0 .../src/plugins/quit.js | 0 .../src/plugins/test_name_pattern.js | 0 .../src/plugins/test_path_pattern.js | 0 .../src/plugins/update_snapshots.js | 0 .../plugins/update_snapshots_interactive.js | 0 .../{jest-cli => jest-core}/src/pluralize.js | 0 .../src/runGlobalHook.js | 0 .../{jest-cli => jest-core}/src/runJest.js | 0 .../src/testResultHelpers.js | 0 .../src/testSchedulerHelper.js | 0 packages/{jest-cli => jest-core}/src/types.js | 0 packages/{jest-cli => jest-core}/src/watch.js | 0 .../src/__tests__/testRunner.test.js | 6 +- packages/jest/src/jest.js | 2 +- types/TestRunner.js | 8 +- yarn.lock | 9 +- 101 files changed, 386 insertions(+), 317 deletions(-) create mode 100644 packages/jest-cli/src/index.js rename packages/jest-cli/src/{lib => init}/__tests__/__snapshots__/init.test.js.snap (100%) rename packages/jest-cli/src/{lib => init}/__tests__/__snapshots__/modify_package_json.test.js.snap (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/has_jest_config_file/jest.config.js (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/has_jest_config_file/package.json (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/has_jest_config_in_package_json/package.json (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/no_package_json/index.js (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/only_package_json/package.json (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/test_script_configured/package.json (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/typescript_in_dependencies/package.json (100%) rename packages/jest-cli/src/{lib => init}/__tests__/fixtures/typescript_in_dev_dependencies/package.json (100%) rename packages/jest-cli/src/{lib => init}/__tests__/init.test.js (99%) rename packages/jest-cli/src/{lib => init}/__tests__/modify_package_json.test.js (94%) rename packages/jest-cli/src/{ => init}/constants.js (100%) rename packages/jest-cli/src/{lib => }/init/errors.js (100%) rename packages/jest-cli/src/{lib => }/init/generate_config_file.js (100%) rename packages/jest-cli/src/{lib => }/init/index.js (98%) rename packages/jest-cli/src/{lib => }/init/modify_package_json.js (100%) rename packages/jest-cli/src/{lib => }/init/questions.js (100%) delete mode 100644 packages/jest-cli/src/jest.js create mode 100644 packages/jest-core/.npmignore create mode 100644 packages/jest-core/README.md create mode 100644 packages/jest-core/package.json rename packages/{jest-cli => jest-core}/src/FailedTestsCache.js (100%) rename packages/{jest-cli => jest-core}/src/ReporterDispatcher.js (100%) rename packages/{jest-cli => jest-core}/src/SearchSource.js (100%) rename packages/{jest-cli => jest-core}/src/SnapshotInteractiveMode.js (100%) rename packages/{jest-cli => jest-core}/src/TestNamePatternPrompt.js (100%) rename packages/{jest-cli => jest-core}/src/TestPathPatternPrompt.js (100%) rename packages/{jest-cli => jest-core}/src/TestScheduler.js (100%) rename packages/{jest-cli => jest-core}/src/TestSequencer.js (100%) rename packages/{jest-cli => jest-core}/src/TestWatcher.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/FailedTestsCache.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/SearchSource.test.js (98%) rename packages/{jest-cli => jest-core}/src/__tests__/SnapshotInteractiveMode.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/TestScheduler.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/__fixtures__/watch_plugin.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/__fixtures__/watch_plugin2.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/__snapshots__/SnapshotInteractiveMode.test.js.snap (100%) rename packages/{jest-cli => jest-core}/src/__tests__/__snapshots__/watch.test.js.snap (100%) rename packages/{jest-cli => jest-core}/src/__tests__/__snapshots__/watch_filename_pattern_mode.test.js.snap (100%) rename packages/{jest-cli => jest-core}/src/__tests__/__snapshots__/watch_test_name_pattern_mode.test.js.snap (100%) rename packages/{jest-cli => jest-core}/src/__tests__/globals.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/run_jest.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/testSchedulerHelper.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/.hiddenFolder/not-really-a-test.txt (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/__testtests__/do-not-match-me.txt (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/__testtests__/not-really-a-test.txt (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/__testtests__/test.foobar (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/__testtests__/test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/__testtests__/test.jsx (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/module.foobar (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/module.jsx (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root/no_tests.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root_with_(parentheses)/__testtests__/test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_root_with_(parentheses)/module.jsx (100%) rename packages/{jest-cli => jest-core}/src/__tests__/test_sequencer.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/watch.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/watch_filename_pattern_mode.test.js (100%) rename packages/{jest-cli => jest-core}/src/__tests__/watch_test_name_pattern_mode.test.js (100%) rename packages/{jest-cli => jest-core}/src/assets/jest_logo.png (100%) create mode 100644 packages/jest-core/src/cli/index.js rename packages/{jest-cli => jest-core}/src/collectHandles.js (100%) rename packages/{jest-cli => jest-core}/src/coverage.template (100%) rename packages/{jest-cli => jest-core}/src/getChangedFilesPromise.js (100%) rename packages/{jest-cli => jest-core}/src/getNoTestFound.js (100%) rename packages/{jest-cli => jest-core}/src/getNoTestFoundFailed.js (100%) rename packages/{jest-cli => jest-core}/src/getNoTestFoundRelatedToChangedFiles.js (100%) rename packages/{jest-cli => jest-core}/src/getNoTestFoundVerbose.js (100%) rename packages/{jest-cli => jest-core}/src/getNoTestsFoundMessage.js (100%) create mode 100644 packages/jest-core/src/jest.js rename packages/{jest-cli => jest-core}/src/lib/__tests__/__snapshots__/log_debug_messages.test.js.snap (100%) rename packages/{jest-cli => jest-core}/src/lib/__tests__/is_valid_path.test.js (100%) rename packages/{jest-cli => jest-core}/src/lib/__tests__/log_debug_messages.test.js (100%) rename packages/{jest-cli => jest-core}/src/lib/active_filters_message.js (100%) rename packages/{jest-cli => jest-core}/src/lib/create_context.js (100%) rename packages/{jest-cli => jest-core}/src/lib/handle_deprecation_warnings.js (100%) rename packages/{jest-cli => jest-core}/src/lib/is_valid_path.js (100%) rename packages/{jest-cli => jest-core}/src/lib/log_debug_messages.js (100%) rename packages/{jest-cli => jest-core}/src/lib/update_global_config.js (100%) rename packages/{jest-cli => jest-core}/src/lib/watch_plugins_helpers.js (100%) rename packages/{jest-cli => jest-core}/src/plugins/quit.js (100%) rename packages/{jest-cli => jest-core}/src/plugins/test_name_pattern.js (100%) rename packages/{jest-cli => jest-core}/src/plugins/test_path_pattern.js (100%) rename packages/{jest-cli => jest-core}/src/plugins/update_snapshots.js (100%) rename packages/{jest-cli => jest-core}/src/plugins/update_snapshots_interactive.js (100%) rename packages/{jest-cli => jest-core}/src/pluralize.js (100%) rename packages/{jest-cli => jest-core}/src/runGlobalHook.js (100%) rename packages/{jest-cli => jest-core}/src/runJest.js (100%) rename packages/{jest-cli => jest-core}/src/testResultHelpers.js (100%) rename packages/{jest-cli => jest-core}/src/testSchedulerHelper.js (100%) rename packages/{jest-cli => jest-core}/src/types.js (100%) rename packages/{jest-cli => jest-core}/src/watch.js (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index b490b297a6f5..3a0218561cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ ### Chore & Maintenance +- `[jest-cli]` [**BREAKING**] Move everything not related to CLI args parsing into a new `jest-core` package ([#7696](https://github.com/facebook/jest/pull/7696)) - `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855)) - `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809)) - `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820)) @@ -42,6 +43,7 @@ - `[jest-snapshot]`: Migrate to TypeScript ([#7899](https://github.com/facebook/jest/pull/7899)) - `[@jest/transform]`: New package extracted from `jest-runtime` ([#7915](https://github.com/facebook/jest/pull/7915)) - `[babel-plugin-jest-hoist]`: Migrate to TypeScript ([#7898](https://github.com/facebook/jest/pull/7898)) +- `[jest-core]` Create new package, which is `jest-cli` minus `yargs` and `prompts` ([#7696](https://github.com/facebook/jest/pull/7696)) ### Performance diff --git a/jest.config.js b/jest.config.js index db041ad091ae..7bda2df4675a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -39,9 +39,9 @@ module.exports = { '/packages/.*/build', '/packages/.*/build-es5', '/packages/.*/src/__tests__/setPrettyPrint.ts', - '/packages/jest-cli/src/__tests__/test_root', - '/packages/jest-cli/src/__tests__/__fixtures__/', - '/packages/jest-cli/src/lib/__tests__/fixtures/', + '/packages/jest-core/src/__tests__/test_root', + '/packages/jest-core/src/__tests__/__fixtures__/', + '/packages/jest-cli/src/init/__tests__/fixtures/', '/packages/jest-haste-map/src/__tests__/haste_impl.js', '/packages/jest-haste-map/src/__tests__/dependencyExtractor.js', '/packages/jest-resolve-dependencies/src/__tests__/__fixtures__/', diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 4267412020a5..3b37a7743958 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -2,54 +2,24 @@ "name": "jest-cli", "description": "Delightful JavaScript Testing.", "version": "24.1.0", - "main": "build/jest.js", + "main": "build/index.js", "dependencies": { - "@jest/reporters": "^24.1.0", - "@jest/transform": "^24.1.0", - "ansi-escapes": "^3.0.0", + "@jest/core": "^24.1.0", "chalk": "^2.0.1", "exit": "^0.1.2", - "glob": "^7.1.2", - "graceful-fs": "^4.1.15", "import-local": "^2.0.0", "is-ci": "^2.0.0", - "jest-changed-files": "^24.0.0", "jest-config": "^24.1.0", - "jest-environment-jsdom": "^24.0.0", - "jest-get-type": "^24.0.0", - "jest-haste-map": "^24.0.0", - "jest-message-util": "^24.0.0", - "jest-regex-util": "^24.0.0", - "jest-resolve-dependencies": "^24.1.0", - "jest-runner": "^24.1.0", - "jest-runtime": "^24.1.0", - "jest-snapshot": "^24.1.0", "jest-util": "^24.0.0", "jest-validate": "^24.0.0", - "jest-watcher": "^24.0.0", - "micromatch": "^3.1.10", - "p-each-series": "^1.0.0", - "pirates": "^4.0.0", "prompts": "^2.0.1", "realpath-native": "^1.1.0", - "rimraf": "^2.5.4", - "string-length": "^2.0.0", - "strip-ansi": "^5.0.0", - "which": "^1.2.12", "yargs": "^12.0.2" }, "devDependencies": { - "@types/ansi-escapes": "^3.0.0", "@types/exit": "^0.1.30", - "@types/glob": "^7.1.1", - "@types/graceful-fs": "^4.1.2", "@types/is-ci": "^1.1.0", - "@types/micromatch": "^3.1.0", "@types/prompts": "^1.2.0", - "@types/rimraf": "^2.0.2", - "@types/string-length": "^2.0.0", - "@types/strip-ansi": "^3.0.0", - "@types/which": "^1.3.1", "@types/yargs": "^12.0.2" }, "bin": { diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 69a3ba889d78..910cc7f10fa8 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -12,34 +12,23 @@ import type {Argv} from 'types/Argv'; import type {GlobalConfig, Path} from 'types/Config'; import path from 'path'; -import {Console, clearLine, createDirectory, preRunMessage} from 'jest-util'; +import {clearLine} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; -import {readConfigs, deprecationEntries} from 'jest-config'; +import {deprecationEntries} from 'jest-config'; +import {runCLI} from '@jest/core'; import * as args from './args'; import chalk from 'chalk'; -import createContext from '../lib/create_context'; import exit from 'exit'; -import getChangedFilesPromise from '../getChangedFilesPromise'; -import {formatHandleErrors} from '../collectHandles'; -import handleDeprecationWarnings from '../lib/handle_deprecation_warnings'; -import runJest from '../runJest'; -import Runtime from 'jest-runtime'; -import TestWatcher from '../TestWatcher'; -import watch from '../watch'; -import pluralize from '../pluralize'; import yargs from 'yargs'; -import rimraf from 'rimraf'; import {sync as realpath} from 'realpath-native'; -import init from '../lib/init'; -import logDebugMessages from '../lib/log_debug_messages'; -import getVersion from '../version'; +import init from '../init'; -const {print: preRunMessagePrint} = preRunMessage; +import {version as VERSION} from '../../package.json'; export async function run(maybeArgv?: Argv, project?: Path) { try { // $FlowFixMe:`allow reduced return - const argv: Argv = buildArgv(maybeArgv, project); + const argv: Argv = buildArgv(maybeArgv); if (argv.init) { await init(); @@ -59,129 +48,9 @@ export async function run(maybeArgv?: Argv, project?: Path) { } } -export const runCLI = async ( - argv: Argv, - projects: Array, -): Promise<{results: AggregatedResult, globalConfig: GlobalConfig}> => { - const realFs = require('fs'); - const fs = require('graceful-fs'); - fs.gracefulify(realFs); - - let results; - - // If we output a JSON object, we can't write anything to stdout, since - // it'll break the JSON structure and it won't be valid. - const outputStream = - argv.json || argv.useStderr ? process.stderr : process.stdout; - - const {globalConfig, configs, hasDeprecationWarnings} = readConfigs( - argv, - projects, - ); - - if (argv.debug) { - logDebugMessages(globalConfig, configs, outputStream); - } - - if (argv.showConfig) { - logDebugMessages(globalConfig, configs, process.stdout); - exit(0); - } - - if (argv.clearCache) { - configs.forEach(config => { - rimraf.sync(config.cacheDirectory); - process.stdout.write(`Cleared ${config.cacheDirectory}\n`); - }); - - exit(0); - } - - await _run( - globalConfig, - configs, - hasDeprecationWarnings, - outputStream, - (r: AggregatedResult) => (results = r), - ); - - if (argv.watch || argv.watchAll) { - // If in watch mode, return the promise that will never resolve. - // If the watch mode is interrupted, watch should handle the process - // shutdown. - return new Promise(() => {}); - } - - if (!results) { - throw new Error( - 'AggregatedResult must be present after test run is complete', - ); - } - - const {openHandles} = results; - - if (openHandles && openHandles.length) { - const formatted = formatHandleErrors(openHandles, configs[0]); - - const openHandlesString = pluralize('open handle', formatted.length, 's'); - - const message = - chalk.red( - `\nJest has detected the following ${openHandlesString} potentially keeping Jest from exiting:\n\n`, - ) + formatted.join('\n\n'); - - console.error(message); - } - - return Promise.resolve({globalConfig, results}); -}; - -const readResultsAndExit = ( - result: ?AggregatedResult, - globalConfig: GlobalConfig, -) => { - const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; - - // Only exit if needed - process.on('exit', () => { - if (typeof code === 'number' && code !== 0) { - process.exitCode = code; - } - }); - - if (globalConfig.forceExit) { - if (!globalConfig.detectOpenHandles) { - console.error( - chalk.red.bold('Force exiting Jest\n\n') + - chalk.red( - 'Have you considered using `--detectOpenHandles` to detect ' + - 'async operations that kept running after all tests finished?', - ), - ); - } - - exit(code); - } else if (!globalConfig.detectOpenHandles) { - setTimeout(() => { - console.error( - chalk.red.bold( - 'Jest did not exit one second after the test run has completed.\n\n', - ) + - chalk.red( - 'This usually means that there are asynchronous operations that ' + - "weren't stopped in your tests. Consider running Jest with " + - '`--detectOpenHandles` to troubleshoot this issue.', - ), - ); - // $FlowFixMe: `unref` exists in Node - }, 1000).unref(); - } -}; - -export const buildArgv = (maybeArgv: ?Argv, project: ?Path) => { +export const buildArgv = (maybeArgv: ?Argv) => { const version = - getVersion() + - (__dirname.includes(`packages${path.sep}jest-cli`) ? '-dev' : ''); + VERSION + (__dirname.includes(`packages${path.sep}jest-cli`) ? '-dev' : ''); const rawArgv: Argv | string[] = maybeArgv || process.argv.slice(2); const argv: Argv = yargs(rawArgv) @@ -234,108 +103,44 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => { return projects; }; -const buildContextsAndHasteMaps = async ( - configs, - globalConfig, - outputStream, -) => { - const hasteMapInstances = Array(configs.length); - const contexts = await Promise.all( - configs.map(async (config, index) => { - createDirectory(config.cacheDirectory); - const hasteMapInstance = Runtime.createHasteMap(config, { - console: new Console(outputStream, outputStream), - maxWorkers: globalConfig.maxWorkers, - resetCache: !config.cache, - watch: globalConfig.watch || globalConfig.watchAll, - watchman: globalConfig.watchman, - }); - hasteMapInstances[index] = hasteMapInstance; - return createContext(config, await hasteMapInstance.build()); - }), - ); - - return {contexts, hasteMapInstances}; -}; - -const _run = async ( - globalConfig, - configs, - hasDeprecationWarnings, - outputStream, - onComplete, +const readResultsAndExit = ( + result: ?AggregatedResult, + globalConfig: GlobalConfig, ) => { - // Queries to hg/git can take a while, so we need to start the process - // as soon as possible, so by the time we need the result it's already there. - const changedFilesPromise = getChangedFilesPromise(globalConfig, configs); + const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; - const {contexts, hasteMapInstances} = await buildContextsAndHasteMaps( - configs, - globalConfig, - outputStream, - ); + // Only exit if needed + process.on('exit', () => { + if (typeof code === 'number' && code !== 0) { + process.exitCode = code; + } + }); - globalConfig.watch || globalConfig.watchAll - ? await runWatch( - contexts, - configs, - hasDeprecationWarnings, - globalConfig, - outputStream, - hasteMapInstances, - changedFilesPromise, - ) - : await runWithoutWatch( - globalConfig, - contexts, - outputStream, - onComplete, - changedFilesPromise, + if (globalConfig.forceExit) { + if (!globalConfig.detectOpenHandles) { + console.error( + chalk.red.bold('Force exiting Jest\n\n') + + chalk.red( + 'Have you considered using `--detectOpenHandles` to detect ' + + 'async operations that kept running after all tests finished?', + ), ); -}; - -const runWatch = async ( - contexts, - configs, - hasDeprecationWarnings, - globalConfig, - outputStream, - hasteMapInstances, - changedFilesPromise, -) => { - if (hasDeprecationWarnings) { - try { - await handleDeprecationWarnings(outputStream, process.stdin); - return watch(globalConfig, contexts, outputStream, hasteMapInstances); - } catch (e) { - exit(0); } - } - - return watch(globalConfig, contexts, outputStream, hasteMapInstances); -}; -const runWithoutWatch = async ( - globalConfig, - contexts, - outputStream, - onComplete, - changedFilesPromise, -) => { - const startRun = async () => { - if (!globalConfig.listTests) { - preRunMessagePrint(outputStream); - } - return await runJest({ - changedFilesPromise, - contexts, - failedTestsCache: null, - globalConfig, - onComplete, - outputStream, - startRun, - testWatcher: new TestWatcher({isWatchMode: false}), - }); - }; - return await startRun(); + exit(code); + } else if (!globalConfig.detectOpenHandles) { + setTimeout(() => { + console.error( + chalk.red.bold( + 'Jest did not exit one second after the test run has completed.\n\n', + ) + + chalk.red( + 'This usually means that there are asynchronous operations that ' + + "weren't stopped in your tests. Consider running Jest with " + + '`--detectOpenHandles` to troubleshoot this issue.', + ), + ); + // $FlowFixMe: `unref` exists in Node + }, 1000).unref(); + } }; diff --git a/packages/jest-cli/src/index.js b/packages/jest-cli/src/index.js new file mode 100644 index 000000000000..994257106ce6 --- /dev/null +++ b/packages/jest-cli/src/index.js @@ -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. + * + * @flow + */ + +export {run} from './cli'; diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap similarity index 100% rename from packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap rename to packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/modify_package_json.test.js.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/modify_package_json.test.js.snap similarity index 100% rename from packages/jest-cli/src/lib/__tests__/__snapshots__/modify_package_json.test.js.snap rename to packages/jest-cli/src/init/__tests__/__snapshots__/modify_package_json.test.js.snap diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/has_jest_config_file/jest.config.js b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file/jest.config.js similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/has_jest_config_file/jest.config.js rename to packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file/jest.config.js diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/has_jest_config_file/package.json b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file/package.json similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/has_jest_config_file/package.json rename to packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_file/package.json diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/has_jest_config_in_package_json/package.json b/packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_in_package_json/package.json similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/has_jest_config_in_package_json/package.json rename to packages/jest-cli/src/init/__tests__/fixtures/has_jest_config_in_package_json/package.json diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/no_package_json/index.js b/packages/jest-cli/src/init/__tests__/fixtures/no_package_json/index.js similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/no_package_json/index.js rename to packages/jest-cli/src/init/__tests__/fixtures/no_package_json/index.js diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/only_package_json/package.json b/packages/jest-cli/src/init/__tests__/fixtures/only_package_json/package.json similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/only_package_json/package.json rename to packages/jest-cli/src/init/__tests__/fixtures/only_package_json/package.json diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/test_script_configured/package.json b/packages/jest-cli/src/init/__tests__/fixtures/test_script_configured/package.json similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/test_script_configured/package.json rename to packages/jest-cli/src/init/__tests__/fixtures/test_script_configured/package.json diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/typescript_in_dependencies/package.json b/packages/jest-cli/src/init/__tests__/fixtures/typescript_in_dependencies/package.json similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/typescript_in_dependencies/package.json rename to packages/jest-cli/src/init/__tests__/fixtures/typescript_in_dependencies/package.json diff --git a/packages/jest-cli/src/lib/__tests__/fixtures/typescript_in_dev_dependencies/package.json b/packages/jest-cli/src/init/__tests__/fixtures/typescript_in_dev_dependencies/package.json similarity index 100% rename from packages/jest-cli/src/lib/__tests__/fixtures/typescript_in_dev_dependencies/package.json rename to packages/jest-cli/src/init/__tests__/fixtures/typescript_in_dev_dependencies/package.json diff --git a/packages/jest-cli/src/lib/__tests__/init.test.js b/packages/jest-cli/src/init/__tests__/init.test.js similarity index 99% rename from packages/jest-cli/src/lib/__tests__/init.test.js rename to packages/jest-cli/src/init/__tests__/init.test.js index 277fa89d5ffc..d10629e6153a 100644 --- a/packages/jest-cli/src/lib/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -11,7 +11,7 @@ import fs from 'fs'; import path from 'path'; import prompts from 'prompts'; -import init from '../init'; +import init from '../'; jest.mock('prompts'); jest.mock('../../../../jest-config/build/getCacheDirectory', () => () => diff --git a/packages/jest-cli/src/lib/__tests__/modify_package_json.test.js b/packages/jest-cli/src/init/__tests__/modify_package_json.test.js similarity index 94% rename from packages/jest-cli/src/lib/__tests__/modify_package_json.test.js rename to packages/jest-cli/src/init/__tests__/modify_package_json.test.js index bd1ce0cf0a21..aa2f8f4641f4 100644 --- a/packages/jest-cli/src/lib/__tests__/modify_package_json.test.js +++ b/packages/jest-cli/src/init/__tests__/modify_package_json.test.js @@ -6,7 +6,7 @@ * */ -import modifyPackageJson from '../init/modify_package_json'; +import modifyPackageJson from '../modify_package_json'; test('should remove jest config if exists', () => { expect( diff --git a/packages/jest-cli/src/constants.js b/packages/jest-cli/src/init/constants.js similarity index 100% rename from packages/jest-cli/src/constants.js rename to packages/jest-cli/src/init/constants.js diff --git a/packages/jest-cli/src/lib/init/errors.js b/packages/jest-cli/src/init/errors.js similarity index 100% rename from packages/jest-cli/src/lib/init/errors.js rename to packages/jest-cli/src/init/errors.js diff --git a/packages/jest-cli/src/lib/init/generate_config_file.js b/packages/jest-cli/src/init/generate_config_file.js similarity index 100% rename from packages/jest-cli/src/lib/init/generate_config_file.js rename to packages/jest-cli/src/init/generate_config_file.js diff --git a/packages/jest-cli/src/lib/init/index.js b/packages/jest-cli/src/init/index.js similarity index 98% rename from packages/jest-cli/src/lib/init/index.js rename to packages/jest-cli/src/init/index.js index 4ae03318470e..68dd9f4fa640 100644 --- a/packages/jest-cli/src/lib/init/index.js +++ b/packages/jest-cli/src/init/index.js @@ -14,7 +14,7 @@ import prompts from 'prompts'; import {sync as realpath} from 'realpath-native'; import defaultQuestions, {testScriptQuestion} from './questions'; import {NotFoundPackageJsonError, MalformedPackageJsonError} from './errors'; -import {PACKAGE_JSON, JEST_CONFIG} from '../../constants'; +import {PACKAGE_JSON, JEST_CONFIG} from './constants'; import generateConfigFile from './generate_config_file'; import modifyPackageJson from './modify_package_json'; diff --git a/packages/jest-cli/src/lib/init/modify_package_json.js b/packages/jest-cli/src/init/modify_package_json.js similarity index 100% rename from packages/jest-cli/src/lib/init/modify_package_json.js rename to packages/jest-cli/src/init/modify_package_json.js diff --git a/packages/jest-cli/src/lib/init/questions.js b/packages/jest-cli/src/init/questions.js similarity index 100% rename from packages/jest-cli/src/lib/init/questions.js rename to packages/jest-cli/src/init/questions.js diff --git a/packages/jest-cli/src/jest.js b/packages/jest-cli/src/jest.js deleted file mode 100644 index 22210bef096e..000000000000 --- a/packages/jest-cli/src/jest.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * 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 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, - run, - runCLI, -}; diff --git a/packages/jest-core/.npmignore b/packages/jest-core/.npmignore new file mode 100644 index 000000000000..85e48fe7b0a4 --- /dev/null +++ b/packages/jest-core/.npmignore @@ -0,0 +1,3 @@ +**/__mocks__/** +**/__tests__/** +src diff --git a/packages/jest-core/README.md b/packages/jest-core/README.md new file mode 100644 index 000000000000..d97aaf905e37 --- /dev/null +++ b/packages/jest-core/README.md @@ -0,0 +1,3 @@ +# jest-core + +Jest is currently working on providing a programmatic API. This is under developemnt, and usage of this package directly is currently not supported. diff --git a/packages/jest-core/package.json b/packages/jest-core/package.json new file mode 100644 index 000000000000..824efb42b9b9 --- /dev/null +++ b/packages/jest-core/package.json @@ -0,0 +1,82 @@ +{ + "name": "@jest/core", + "description": "Delightful JavaScript Testing.", + "version": "24.1.0", + "main": "build/jest.js", + "dependencies": { + "@jest/reporters": "^24.1.0", + "@jest/transform": "^24.1.0", + "ansi-escapes": "^3.0.0", + "chalk": "^2.0.1", + "exit": "^0.1.2", + "glob": "^7.1.2", + "graceful-fs": "^4.1.15", + "jest-changed-files": "^24.0.0", + "jest-config": "^24.1.0", + "jest-environment-jsdom": "^24.0.0", + "jest-get-type": "^24.0.0", + "jest-haste-map": "^24.0.0", + "jest-message-util": "^24.0.0", + "jest-regex-util": "^24.0.0", + "jest-resolve-dependencies": "^24.1.0", + "jest-runner": "^24.1.0", + "jest-runtime": "^24.1.0", + "jest-snapshot": "^24.1.0", + "jest-util": "^24.0.0", + "jest-validate": "^24.0.0", + "jest-watcher": "^24.0.0", + "micromatch": "^3.1.10", + "p-each-series": "^1.0.0", + "pirates": "^4.0.0", + "realpath-native": "^1.1.0", + "rimraf": "^2.5.4", + "strip-ansi": "^5.0.0" + }, + "devDependencies": { + "@types/ansi-escapes": "^3.0.0", + "@types/exit": "^0.1.30", + "@types/glob": "^7.1.1", + "@types/graceful-fs": "^4.1.2", + "@types/micromatch": "^3.1.0", + "@types/rimraf": "^2.0.2" + }, + "engines": { + "node": ">= 6" + }, + "repository": { + "type": "git", + "url": "https://github.com/facebook/jest", + "directory": "packages/jest-core" + }, + "bugs": { + "url": "https://github.com/facebook/jest/issues" + }, + "homepage": "https://jestjs.io/", + "license": "MIT", + "keywords": [ + "ava", + "babel", + "coverage", + "easy", + "expect", + "facebook", + "immersive", + "instant", + "jasmine", + "jest", + "jsdom", + "mocha", + "mocking", + "painless", + "qunit", + "runner", + "sandboxed", + "snapshot", + "tap", + "tape", + "test", + "testing", + "typescript", + "watch" + ] +} diff --git a/packages/jest-cli/src/FailedTestsCache.js b/packages/jest-core/src/FailedTestsCache.js similarity index 100% rename from packages/jest-cli/src/FailedTestsCache.js rename to packages/jest-core/src/FailedTestsCache.js diff --git a/packages/jest-cli/src/ReporterDispatcher.js b/packages/jest-core/src/ReporterDispatcher.js similarity index 100% rename from packages/jest-cli/src/ReporterDispatcher.js rename to packages/jest-core/src/ReporterDispatcher.js diff --git a/packages/jest-cli/src/SearchSource.js b/packages/jest-core/src/SearchSource.js similarity index 100% rename from packages/jest-cli/src/SearchSource.js rename to packages/jest-core/src/SearchSource.js diff --git a/packages/jest-cli/src/SnapshotInteractiveMode.js b/packages/jest-core/src/SnapshotInteractiveMode.js similarity index 100% rename from packages/jest-cli/src/SnapshotInteractiveMode.js rename to packages/jest-core/src/SnapshotInteractiveMode.js diff --git a/packages/jest-cli/src/TestNamePatternPrompt.js b/packages/jest-core/src/TestNamePatternPrompt.js similarity index 100% rename from packages/jest-cli/src/TestNamePatternPrompt.js rename to packages/jest-core/src/TestNamePatternPrompt.js diff --git a/packages/jest-cli/src/TestPathPatternPrompt.js b/packages/jest-core/src/TestPathPatternPrompt.js similarity index 100% rename from packages/jest-cli/src/TestPathPatternPrompt.js rename to packages/jest-core/src/TestPathPatternPrompt.js diff --git a/packages/jest-cli/src/TestScheduler.js b/packages/jest-core/src/TestScheduler.js similarity index 100% rename from packages/jest-cli/src/TestScheduler.js rename to packages/jest-core/src/TestScheduler.js diff --git a/packages/jest-cli/src/TestSequencer.js b/packages/jest-core/src/TestSequencer.js similarity index 100% rename from packages/jest-cli/src/TestSequencer.js rename to packages/jest-core/src/TestSequencer.js diff --git a/packages/jest-cli/src/TestWatcher.js b/packages/jest-core/src/TestWatcher.js similarity index 100% rename from packages/jest-cli/src/TestWatcher.js rename to packages/jest-core/src/TestWatcher.js diff --git a/packages/jest-cli/src/__tests__/FailedTestsCache.test.js b/packages/jest-core/src/__tests__/FailedTestsCache.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/FailedTestsCache.test.js rename to packages/jest-core/src/__tests__/FailedTestsCache.test.js diff --git a/packages/jest-cli/src/__tests__/SearchSource.test.js b/packages/jest-core/src/__tests__/SearchSource.test.js similarity index 98% rename from packages/jest-cli/src/__tests__/SearchSource.test.js rename to packages/jest-core/src/__tests__/SearchSource.test.js index 0f010a136e3d..d3ed9b7117a0 100644 --- a/packages/jest-cli/src/__tests__/SearchSource.test.js +++ b/packages/jest-core/src/__tests__/SearchSource.test.js @@ -471,7 +471,7 @@ describe('SearchSource', () => { }); it('finds tests for a single file', () => { - const input = ['packages/jest-cli/src/__tests__/test_root/module.jsx']; + const input = ['packages/jest-core/src/__tests__/test_root/module.jsx']; const data = searchSource.findRelatedTestsFromPattern(input); expect(toPaths(data.tests).sort()).toEqual([ path.join(rootDir, '__testtests__', 'test.js'), @@ -481,8 +481,8 @@ describe('SearchSource', () => { it('finds tests for multiple files', () => { const input = [ - 'packages/jest-cli/src/__tests__/test_root/module.jsx', - 'packages/jest-cli/src/__tests__/test_root/module.foobar', + 'packages/jest-core/src/__tests__/test_root/module.jsx', + 'packages/jest-core/src/__tests__/test_root/module.foobar', ]; const data = searchSource.findRelatedTestsFromPattern(input); expect(toPaths(data.tests).sort()).toEqual([ diff --git a/packages/jest-cli/src/__tests__/SnapshotInteractiveMode.test.js b/packages/jest-core/src/__tests__/SnapshotInteractiveMode.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/SnapshotInteractiveMode.test.js rename to packages/jest-core/src/__tests__/SnapshotInteractiveMode.test.js diff --git a/packages/jest-cli/src/__tests__/TestScheduler.test.js b/packages/jest-core/src/__tests__/TestScheduler.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/TestScheduler.test.js rename to packages/jest-core/src/__tests__/TestScheduler.test.js diff --git a/packages/jest-cli/src/__tests__/__fixtures__/watch_plugin.js b/packages/jest-core/src/__tests__/__fixtures__/watch_plugin.js similarity index 100% rename from packages/jest-cli/src/__tests__/__fixtures__/watch_plugin.js rename to packages/jest-core/src/__tests__/__fixtures__/watch_plugin.js diff --git a/packages/jest-cli/src/__tests__/__fixtures__/watch_plugin2.js b/packages/jest-core/src/__tests__/__fixtures__/watch_plugin2.js similarity index 100% rename from packages/jest-cli/src/__tests__/__fixtures__/watch_plugin2.js rename to packages/jest-core/src/__tests__/__fixtures__/watch_plugin2.js diff --git a/packages/jest-cli/src/__tests__/__snapshots__/SnapshotInteractiveMode.test.js.snap b/packages/jest-core/src/__tests__/__snapshots__/SnapshotInteractiveMode.test.js.snap similarity index 100% rename from packages/jest-cli/src/__tests__/__snapshots__/SnapshotInteractiveMode.test.js.snap rename to packages/jest-core/src/__tests__/__snapshots__/SnapshotInteractiveMode.test.js.snap diff --git a/packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap b/packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap similarity index 100% rename from packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap rename to packages/jest-core/src/__tests__/__snapshots__/watch.test.js.snap diff --git a/packages/jest-cli/src/__tests__/__snapshots__/watch_filename_pattern_mode.test.js.snap b/packages/jest-core/src/__tests__/__snapshots__/watch_filename_pattern_mode.test.js.snap similarity index 100% rename from packages/jest-cli/src/__tests__/__snapshots__/watch_filename_pattern_mode.test.js.snap rename to packages/jest-core/src/__tests__/__snapshots__/watch_filename_pattern_mode.test.js.snap diff --git a/packages/jest-cli/src/__tests__/__snapshots__/watch_test_name_pattern_mode.test.js.snap b/packages/jest-core/src/__tests__/__snapshots__/watch_test_name_pattern_mode.test.js.snap similarity index 100% rename from packages/jest-cli/src/__tests__/__snapshots__/watch_test_name_pattern_mode.test.js.snap rename to packages/jest-core/src/__tests__/__snapshots__/watch_test_name_pattern_mode.test.js.snap diff --git a/packages/jest-cli/src/__tests__/globals.test.js b/packages/jest-core/src/__tests__/globals.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/globals.test.js rename to packages/jest-core/src/__tests__/globals.test.js diff --git a/packages/jest-cli/src/__tests__/run_jest.test.js b/packages/jest-core/src/__tests__/run_jest.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/run_jest.test.js rename to packages/jest-core/src/__tests__/run_jest.test.js diff --git a/packages/jest-cli/src/__tests__/testSchedulerHelper.test.js b/packages/jest-core/src/__tests__/testSchedulerHelper.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/testSchedulerHelper.test.js rename to packages/jest-core/src/__tests__/testSchedulerHelper.test.js diff --git a/packages/jest-cli/src/__tests__/test_root/.hiddenFolder/not-really-a-test.txt b/packages/jest-core/src/__tests__/test_root/.hiddenFolder/not-really-a-test.txt similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/.hiddenFolder/not-really-a-test.txt rename to packages/jest-core/src/__tests__/test_root/.hiddenFolder/not-really-a-test.txt diff --git a/packages/jest-cli/src/__tests__/test_root/__testtests__/do-not-match-me.txt b/packages/jest-core/src/__tests__/test_root/__testtests__/do-not-match-me.txt similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/__testtests__/do-not-match-me.txt rename to packages/jest-core/src/__tests__/test_root/__testtests__/do-not-match-me.txt diff --git a/packages/jest-cli/src/__tests__/test_root/__testtests__/not-really-a-test.txt b/packages/jest-core/src/__tests__/test_root/__testtests__/not-really-a-test.txt similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/__testtests__/not-really-a-test.txt rename to packages/jest-core/src/__tests__/test_root/__testtests__/not-really-a-test.txt diff --git a/packages/jest-cli/src/__tests__/test_root/__testtests__/test.foobar b/packages/jest-core/src/__tests__/test_root/__testtests__/test.foobar similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/__testtests__/test.foobar rename to packages/jest-core/src/__tests__/test_root/__testtests__/test.foobar diff --git a/packages/jest-cli/src/__tests__/test_root/__testtests__/test.js b/packages/jest-core/src/__tests__/test_root/__testtests__/test.js similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/__testtests__/test.js rename to packages/jest-core/src/__tests__/test_root/__testtests__/test.js diff --git a/packages/jest-cli/src/__tests__/test_root/__testtests__/test.jsx b/packages/jest-core/src/__tests__/test_root/__testtests__/test.jsx similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/__testtests__/test.jsx rename to packages/jest-core/src/__tests__/test_root/__testtests__/test.jsx diff --git a/packages/jest-cli/src/__tests__/test_root/module.foobar b/packages/jest-core/src/__tests__/test_root/module.foobar similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/module.foobar rename to packages/jest-core/src/__tests__/test_root/module.foobar diff --git a/packages/jest-cli/src/__tests__/test_root/module.jsx b/packages/jest-core/src/__tests__/test_root/module.jsx similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/module.jsx rename to packages/jest-core/src/__tests__/test_root/module.jsx diff --git a/packages/jest-cli/src/__tests__/test_root/no_tests.js b/packages/jest-core/src/__tests__/test_root/no_tests.js similarity index 100% rename from packages/jest-cli/src/__tests__/test_root/no_tests.js rename to packages/jest-core/src/__tests__/test_root/no_tests.js diff --git a/packages/jest-cli/src/__tests__/test_root_with_(parentheses)/__testtests__/test.js b/packages/jest-core/src/__tests__/test_root_with_(parentheses)/__testtests__/test.js similarity index 100% rename from packages/jest-cli/src/__tests__/test_root_with_(parentheses)/__testtests__/test.js rename to packages/jest-core/src/__tests__/test_root_with_(parentheses)/__testtests__/test.js diff --git a/packages/jest-cli/src/__tests__/test_root_with_(parentheses)/module.jsx b/packages/jest-core/src/__tests__/test_root_with_(parentheses)/module.jsx similarity index 100% rename from packages/jest-cli/src/__tests__/test_root_with_(parentheses)/module.jsx rename to packages/jest-core/src/__tests__/test_root_with_(parentheses)/module.jsx diff --git a/packages/jest-cli/src/__tests__/test_sequencer.test.js b/packages/jest-core/src/__tests__/test_sequencer.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/test_sequencer.test.js rename to packages/jest-core/src/__tests__/test_sequencer.test.js diff --git a/packages/jest-cli/src/__tests__/watch.test.js b/packages/jest-core/src/__tests__/watch.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/watch.test.js rename to packages/jest-core/src/__tests__/watch.test.js diff --git a/packages/jest-cli/src/__tests__/watch_filename_pattern_mode.test.js b/packages/jest-core/src/__tests__/watch_filename_pattern_mode.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/watch_filename_pattern_mode.test.js rename to packages/jest-core/src/__tests__/watch_filename_pattern_mode.test.js diff --git a/packages/jest-cli/src/__tests__/watch_test_name_pattern_mode.test.js b/packages/jest-core/src/__tests__/watch_test_name_pattern_mode.test.js similarity index 100% rename from packages/jest-cli/src/__tests__/watch_test_name_pattern_mode.test.js rename to packages/jest-core/src/__tests__/watch_test_name_pattern_mode.test.js diff --git a/packages/jest-cli/src/assets/jest_logo.png b/packages/jest-core/src/assets/jest_logo.png similarity index 100% rename from packages/jest-cli/src/assets/jest_logo.png rename to packages/jest-core/src/assets/jest_logo.png diff --git a/packages/jest-core/src/cli/index.js b/packages/jest-core/src/cli/index.js new file mode 100644 index 000000000000..b71054c167de --- /dev/null +++ b/packages/jest-core/src/cli/index.js @@ -0,0 +1,211 @@ +/** + * 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 type {AggregatedResult} from 'types/TestResult'; +import type {Argv} from 'types/Argv'; +import type {GlobalConfig, Path} from 'types/Config'; + +import {Console, createDirectory, preRunMessage} from 'jest-util'; +import {readConfigs} from 'jest-config'; +import chalk from 'chalk'; +import createContext from '../lib/create_context'; +import exit from 'exit'; +import getChangedFilesPromise from '../getChangedFilesPromise'; +import {formatHandleErrors} from '../collectHandles'; +import handleDeprecationWarnings from '../lib/handle_deprecation_warnings'; +import runJest from '../runJest'; +import Runtime from 'jest-runtime'; +import TestWatcher from '../TestWatcher'; +import watch from '../watch'; +import pluralize from '../pluralize'; +import rimraf from 'rimraf'; +import logDebugMessages from '../lib/log_debug_messages'; + +const {print: preRunMessagePrint} = preRunMessage; + +export const runCLI = async ( + argv: Argv, + projects: Array, +): Promise<{results: AggregatedResult, globalConfig: GlobalConfig}> => { + const realFs = require('fs'); + const fs = require('graceful-fs'); + fs.gracefulify(realFs); + + let results; + + // If we output a JSON object, we can't write anything to stdout, since + // it'll break the JSON structure and it won't be valid. + const outputStream = + argv.json || argv.useStderr ? process.stderr : process.stdout; + + const {globalConfig, configs, hasDeprecationWarnings} = readConfigs( + argv, + projects, + ); + + if (argv.debug) { + logDebugMessages(globalConfig, configs, outputStream); + } + + if (argv.showConfig) { + logDebugMessages(globalConfig, configs, process.stdout); + exit(0); + } + + if (argv.clearCache) { + configs.forEach(config => { + rimraf.sync(config.cacheDirectory); + process.stdout.write(`Cleared ${config.cacheDirectory}\n`); + }); + + exit(0); + } + + await _run( + globalConfig, + configs, + hasDeprecationWarnings, + outputStream, + (r: AggregatedResult) => (results = r), + ); + + if (argv.watch || argv.watchAll) { + // If in watch mode, return the promise that will never resolve. + // If the watch mode is interrupted, watch should handle the process + // shutdown. + return new Promise(() => {}); + } + + if (!results) { + throw new Error( + 'AggregatedResult must be present after test run is complete', + ); + } + + const {openHandles} = results; + + if (openHandles && openHandles.length) { + const formatted = formatHandleErrors(openHandles, configs[0]); + + const openHandlesString = pluralize('open handle', formatted.length, 's'); + + const message = + chalk.red( + `\nJest has detected the following ${openHandlesString} potentially keeping Jest from exiting:\n\n`, + ) + formatted.join('\n\n'); + + console.error(message); + } + + return {globalConfig, results}; +}; + +const buildContextsAndHasteMaps = async ( + configs, + globalConfig, + outputStream, +) => { + const hasteMapInstances = Array(configs.length); + const contexts = await Promise.all( + configs.map(async (config, index) => { + createDirectory(config.cacheDirectory); + const hasteMapInstance = Runtime.createHasteMap(config, { + console: new Console(outputStream, outputStream), + maxWorkers: globalConfig.maxWorkers, + resetCache: !config.cache, + watch: globalConfig.watch || globalConfig.watchAll, + watchman: globalConfig.watchman, + }); + hasteMapInstances[index] = hasteMapInstance; + return createContext(config, await hasteMapInstance.build()); + }), + ); + + return {contexts, hasteMapInstances}; +}; + +const _run = async ( + globalConfig, + configs, + hasDeprecationWarnings, + outputStream, + onComplete, +) => { + // Queries to hg/git can take a while, so we need to start the process + // as soon as possible, so by the time we need the result it's already there. + const changedFilesPromise = getChangedFilesPromise(globalConfig, configs); + + const {contexts, hasteMapInstances} = await buildContextsAndHasteMaps( + configs, + globalConfig, + outputStream, + ); + + globalConfig.watch || globalConfig.watchAll + ? await runWatch( + contexts, + configs, + hasDeprecationWarnings, + globalConfig, + outputStream, + hasteMapInstances, + ) + : await runWithoutWatch( + globalConfig, + contexts, + outputStream, + onComplete, + changedFilesPromise, + ); +}; + +const runWatch = async ( + contexts, + configs, + hasDeprecationWarnings, + globalConfig, + outputStream, + hasteMapInstances, +) => { + if (hasDeprecationWarnings) { + try { + await handleDeprecationWarnings(outputStream, process.stdin); + return watch(globalConfig, contexts, outputStream, hasteMapInstances); + } catch (e) { + exit(0); + } + } + + return watch(globalConfig, contexts, outputStream, hasteMapInstances); +}; + +const runWithoutWatch = async ( + globalConfig, + contexts, + outputStream, + onComplete, + changedFilesPromise, +) => { + const startRun = async () => { + if (!globalConfig.listTests) { + preRunMessagePrint(outputStream); + } + return runJest({ + changedFilesPromise, + contexts, + failedTestsCache: null, + globalConfig, + onComplete, + outputStream, + startRun, + testWatcher: new TestWatcher({isWatchMode: false}), + }); + }; + return startRun(); +}; diff --git a/packages/jest-cli/src/collectHandles.js b/packages/jest-core/src/collectHandles.js similarity index 100% rename from packages/jest-cli/src/collectHandles.js rename to packages/jest-core/src/collectHandles.js diff --git a/packages/jest-cli/src/coverage.template b/packages/jest-core/src/coverage.template similarity index 100% rename from packages/jest-cli/src/coverage.template rename to packages/jest-core/src/coverage.template diff --git a/packages/jest-cli/src/getChangedFilesPromise.js b/packages/jest-core/src/getChangedFilesPromise.js similarity index 100% rename from packages/jest-cli/src/getChangedFilesPromise.js rename to packages/jest-core/src/getChangedFilesPromise.js diff --git a/packages/jest-cli/src/getNoTestFound.js b/packages/jest-core/src/getNoTestFound.js similarity index 100% rename from packages/jest-cli/src/getNoTestFound.js rename to packages/jest-core/src/getNoTestFound.js diff --git a/packages/jest-cli/src/getNoTestFoundFailed.js b/packages/jest-core/src/getNoTestFoundFailed.js similarity index 100% rename from packages/jest-cli/src/getNoTestFoundFailed.js rename to packages/jest-core/src/getNoTestFoundFailed.js diff --git a/packages/jest-cli/src/getNoTestFoundRelatedToChangedFiles.js b/packages/jest-core/src/getNoTestFoundRelatedToChangedFiles.js similarity index 100% rename from packages/jest-cli/src/getNoTestFoundRelatedToChangedFiles.js rename to packages/jest-core/src/getNoTestFoundRelatedToChangedFiles.js diff --git a/packages/jest-cli/src/getNoTestFoundVerbose.js b/packages/jest-core/src/getNoTestFoundVerbose.js similarity index 100% rename from packages/jest-cli/src/getNoTestFoundVerbose.js rename to packages/jest-core/src/getNoTestFoundVerbose.js diff --git a/packages/jest-cli/src/getNoTestsFoundMessage.js b/packages/jest-core/src/getNoTestsFoundMessage.js similarity index 100% rename from packages/jest-cli/src/getNoTestsFoundMessage.js rename to packages/jest-core/src/getNoTestsFoundMessage.js diff --git a/packages/jest-core/src/jest.js b/packages/jest-core/src/jest.js new file mode 100644 index 000000000000..ca6fe2d0d479 --- /dev/null +++ b/packages/jest-core/src/jest.js @@ -0,0 +1,13 @@ +/** + * 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 + */ + +export {default as SearchSource} from './SearchSource'; +export {default as TestScheduler} from './TestScheduler'; +export {default as TestWatcher} from './TestWatcher'; +export {runCLI} from './cli'; diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/log_debug_messages.test.js.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.js.snap similarity index 100% rename from packages/jest-cli/src/lib/__tests__/__snapshots__/log_debug_messages.test.js.snap rename to packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.js.snap diff --git a/packages/jest-cli/src/lib/__tests__/is_valid_path.test.js b/packages/jest-core/src/lib/__tests__/is_valid_path.test.js similarity index 100% rename from packages/jest-cli/src/lib/__tests__/is_valid_path.test.js rename to packages/jest-core/src/lib/__tests__/is_valid_path.test.js diff --git a/packages/jest-cli/src/lib/__tests__/log_debug_messages.test.js b/packages/jest-core/src/lib/__tests__/log_debug_messages.test.js similarity index 100% rename from packages/jest-cli/src/lib/__tests__/log_debug_messages.test.js rename to packages/jest-core/src/lib/__tests__/log_debug_messages.test.js diff --git a/packages/jest-cli/src/lib/active_filters_message.js b/packages/jest-core/src/lib/active_filters_message.js similarity index 100% rename from packages/jest-cli/src/lib/active_filters_message.js rename to packages/jest-core/src/lib/active_filters_message.js diff --git a/packages/jest-cli/src/lib/create_context.js b/packages/jest-core/src/lib/create_context.js similarity index 100% rename from packages/jest-cli/src/lib/create_context.js rename to packages/jest-core/src/lib/create_context.js diff --git a/packages/jest-cli/src/lib/handle_deprecation_warnings.js b/packages/jest-core/src/lib/handle_deprecation_warnings.js similarity index 100% rename from packages/jest-cli/src/lib/handle_deprecation_warnings.js rename to packages/jest-core/src/lib/handle_deprecation_warnings.js diff --git a/packages/jest-cli/src/lib/is_valid_path.js b/packages/jest-core/src/lib/is_valid_path.js similarity index 100% rename from packages/jest-cli/src/lib/is_valid_path.js rename to packages/jest-core/src/lib/is_valid_path.js diff --git a/packages/jest-cli/src/lib/log_debug_messages.js b/packages/jest-core/src/lib/log_debug_messages.js similarity index 100% rename from packages/jest-cli/src/lib/log_debug_messages.js rename to packages/jest-core/src/lib/log_debug_messages.js diff --git a/packages/jest-cli/src/lib/update_global_config.js b/packages/jest-core/src/lib/update_global_config.js similarity index 100% rename from packages/jest-cli/src/lib/update_global_config.js rename to packages/jest-core/src/lib/update_global_config.js diff --git a/packages/jest-cli/src/lib/watch_plugins_helpers.js b/packages/jest-core/src/lib/watch_plugins_helpers.js similarity index 100% rename from packages/jest-cli/src/lib/watch_plugins_helpers.js rename to packages/jest-core/src/lib/watch_plugins_helpers.js diff --git a/packages/jest-cli/src/plugins/quit.js b/packages/jest-core/src/plugins/quit.js similarity index 100% rename from packages/jest-cli/src/plugins/quit.js rename to packages/jest-core/src/plugins/quit.js diff --git a/packages/jest-cli/src/plugins/test_name_pattern.js b/packages/jest-core/src/plugins/test_name_pattern.js similarity index 100% rename from packages/jest-cli/src/plugins/test_name_pattern.js rename to packages/jest-core/src/plugins/test_name_pattern.js diff --git a/packages/jest-cli/src/plugins/test_path_pattern.js b/packages/jest-core/src/plugins/test_path_pattern.js similarity index 100% rename from packages/jest-cli/src/plugins/test_path_pattern.js rename to packages/jest-core/src/plugins/test_path_pattern.js diff --git a/packages/jest-cli/src/plugins/update_snapshots.js b/packages/jest-core/src/plugins/update_snapshots.js similarity index 100% rename from packages/jest-cli/src/plugins/update_snapshots.js rename to packages/jest-core/src/plugins/update_snapshots.js diff --git a/packages/jest-cli/src/plugins/update_snapshots_interactive.js b/packages/jest-core/src/plugins/update_snapshots_interactive.js similarity index 100% rename from packages/jest-cli/src/plugins/update_snapshots_interactive.js rename to packages/jest-core/src/plugins/update_snapshots_interactive.js diff --git a/packages/jest-cli/src/pluralize.js b/packages/jest-core/src/pluralize.js similarity index 100% rename from packages/jest-cli/src/pluralize.js rename to packages/jest-core/src/pluralize.js diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-core/src/runGlobalHook.js similarity index 100% rename from packages/jest-cli/src/runGlobalHook.js rename to packages/jest-core/src/runGlobalHook.js diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-core/src/runJest.js similarity index 100% rename from packages/jest-cli/src/runJest.js rename to packages/jest-core/src/runJest.js diff --git a/packages/jest-cli/src/testResultHelpers.js b/packages/jest-core/src/testResultHelpers.js similarity index 100% rename from packages/jest-cli/src/testResultHelpers.js rename to packages/jest-core/src/testResultHelpers.js diff --git a/packages/jest-cli/src/testSchedulerHelper.js b/packages/jest-core/src/testSchedulerHelper.js similarity index 100% rename from packages/jest-cli/src/testSchedulerHelper.js rename to packages/jest-core/src/testSchedulerHelper.js diff --git a/packages/jest-cli/src/types.js b/packages/jest-core/src/types.js similarity index 100% rename from packages/jest-cli/src/types.js rename to packages/jest-core/src/types.js diff --git a/packages/jest-cli/src/watch.js b/packages/jest-core/src/watch.js similarity index 100% rename from packages/jest-cli/src/watch.js rename to packages/jest-core/src/watch.js diff --git a/packages/jest-runner/src/__tests__/testRunner.test.js b/packages/jest-runner/src/__tests__/testRunner.test.js index c4ce341398cd..ca604b10c0ee 100644 --- a/packages/jest-runner/src/__tests__/testRunner.test.js +++ b/packages/jest-runner/src/__tests__/testRunner.test.js @@ -6,10 +6,8 @@ * */ -'use strict'; - -const TestRunner = require('../index'); -const {TestWatcher} = require('jest-cli'); +import {TestWatcher} from '@jest/core'; +import TestRunner from '../index'; let mockWorkerFarm; diff --git a/packages/jest/src/jest.js b/packages/jest/src/jest.js index 318e1641c408..fe580071a094 100644 --- a/packages/jest/src/jest.js +++ b/packages/jest/src/jest.js @@ -7,6 +7,6 @@ * @flow */ -import cli from 'jest-cli'; +import * as cli from 'jest-cli'; module.exports = cli; diff --git a/types/TestRunner.js b/types/TestRunner.js index 2aa7005c6f26..2588e1a29296 100644 --- a/types/TestRunner.js +++ b/types/TestRunner.js @@ -8,16 +8,16 @@ */ import type {Context} from './Context'; -import type {Environment} from 'types/Environment'; +import type {Environment} from './Environment'; import type {GlobalConfig, Path, ProjectConfig} from './Config'; -import type {ReporterOnStartOptions} from 'types/Reporters'; +import type {ReporterOnStartOptions} from './Reporters'; import type { AggregatedResult, SerializableError, TestResult, -} from 'types/TestResult'; +} from './TestResult'; import type Runtime from 'jest-runtime'; -import type {TestWatcher as _TestWatcher} from 'jest-cli'; +import type {TestWatcher as _TestWatcher} from '@jest/core'; export type Test = {| context: Context, diff --git a/yarn.lock b/yarn.lock index a78b50b6fcd2..2ca7de58e22a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -838,7 +838,7 @@ globals "^11.1.0" lodash "^4.17.10" -"@babel/types@^7.0.0", "@babel/types@^7.1.2", "@babel/types@^7.2.0", "@babel/types@^7.2.2", "@babel/types@^7.3.0": +"@babel/types@^7.0.0", "@babel/types@^7.1.2", "@babel/types@^7.2.0", "@babel/types@^7.2.2", "@babel/types@^7.3.0", "@babel/types@^7.3.3": version "7.3.3" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.3.3.tgz#6c44d1cdac2a7625b624216657d5bc6c107ab436" integrity sha512-2tACZ80Wg09UnPg5uGAOUvvInaqLk3l/IAhQzlxLQOIXacr6bMsra5SH6AWw/hIDRCSbCdHP2KzSOD+cT7TzMQ== @@ -1823,11 +1823,6 @@ dependencies: "@types/node" "*" -"@types/which@^1.3.1": - version "1.3.1" - resolved "https://registry.yarnpkg.com/@types/which/-/which-1.3.1.tgz#7802c380887986ca909008afea4e08025b130f8d" - integrity sha512-ZrJDWpvg75LTGX4XwuneY9s6bF3OeZcGTpoGh3zDV9ytzcHMFsRrMIaLBRJZQMBoGyKs6unBQfVdrLZiYfb1zQ== - "@types/write-file-atomic@^2.1.1": version "2.1.1" resolved "https://registry.yarnpkg.com/@types/write-file-atomic/-/write-file-atomic-2.1.1.tgz#7f9fcd6c5c8d194dba03472e3fa6cb29a839764c" @@ -13307,7 +13302,7 @@ which-module@^2.0.0: resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a" integrity sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho= -which@1, which@^1.2.1, which@^1.2.12, which@^1.2.14, which@^1.2.9, which@^1.3.0, which@^1.3.1: +which@1, which@^1.2.1, which@^1.2.14, which@^1.2.9, which@^1.3.0, which@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==