diff --git a/CHANGELOG.md b/CHANGELOG.md index 34e0a007cb4f..596903cf4ef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,8 @@ * `[jest-environment-jsdom]` Update JSOM to 11.4, which includes built-in support for `requestAnimationFrame` ([#4919](https://github.com/facebook/jest/pull/4919)) +* `[jest-cli]` Hide watch usage output when running on non-interactive + environments ([#4958](https://github.com/facebook/jest/pull/4958)) * `[jest-snapshot]` Promises support for `toThrowErrorMatchingSnapshot` ([#4946](https://github.com/facebook/jest/pull/4946)) * `[jest-cli]` Explain which snapshots are obsolete diff --git a/packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap b/packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap index afb8bf1fa3a9..298b413cdad2 100644 --- a/packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap +++ b/packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Watch mode flows Runs Jest in a non-interactive environment not showing usage 1`] = ` +Array [ + " +", +] +`; + exports[`Watch mode flows Runs Jest once by default and shows usage 1`] = ` Array [ " diff --git a/packages/jest-cli/src/__tests__/watch.test.js b/packages/jest-cli/src/__tests__/watch.test.js index cf4e4cdd8149..c37ec65251b3 100644 --- a/packages/jest-cli/src/__tests__/watch.test.js +++ b/packages/jest-cli/src/__tests__/watch.test.js @@ -101,7 +101,29 @@ describe('Watch mode flows', () => { }); it('Runs Jest once by default and shows usage', () => { - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + jest.unmock('jest-util'); + const util = require('jest-util'); + util.isInteractive = true; + + const ci_watch = require('../watch').default; + ci_watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + expect(runJestMock.mock.calls[0][0]).toMatchObject({ + contexts, + globalConfig, + onComplete: expect.any(Function), + outputStream: pipe, + testWatcher: new TestWatcher({isWatchMode: true}), + }); + expect(pipe.write.mock.calls.reverse()[0]).toMatchSnapshot(); + }); + + it('Runs Jest in a non-interactive environment not showing usage', () => { + jest.unmock('jest-util'); + const util = require('jest-util'); + util.isInteractive = false; + + const ci_watch = require('../watch').default; + ci_watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); expect(runJestMock.mock.calls[0][0]).toMatchObject({ contexts, globalConfig, @@ -128,7 +150,12 @@ describe('Watch mode flows', () => { }); it('shows prompts for WatchPlugins in alphabetical order', async () => { - watch( + jest.unmock('jest-util'); + const util = require('jest-util'); + util.isInteractive = true; + + const ci_watch = require('../watch').default; + ci_watch( Object.assign({}, globalConfig, { rootDir: __dirname, watchPlugins: [watchPlugin2Path, watchPluginPath], diff --git a/packages/jest-cli/src/get_no_test_found_related_to_changed_files.js b/packages/jest-cli/src/get_no_test_found_related_to_changed_files.js index 89a1e55f82cb..bc9aa31d2014 100644 --- a/packages/jest-cli/src/get_no_test_found_related_to_changed_files.js +++ b/packages/jest-cli/src/get_no_test_found_related_to_changed_files.js @@ -1,12 +1,18 @@ import chalk from 'chalk'; +import {isInteractive} from 'jest-util'; export default function getNoTestFoundRelatedToChangedFiles(globalConfig) { - return ( - chalk.bold('No tests found related to files changed since last commit.\n') + - chalk.dim( - globalConfig.watch + let msg = chalk.bold( + 'No tests found related to files changed since last commit.', + ); + + if (isInteractive) { + msg += chalk.dim( + '\n' + globalConfig.watch ? 'Press `a` to run all tests, or run Jest with `--watchAll`.' : 'Run Jest without `-o` or with `--all` to run all tests.', - ) - ); + ); + } + + return msg; } diff --git a/packages/jest-cli/src/pre_run_message.js b/packages/jest-cli/src/pre_run_message.js index 7758d0ca30b6..443d716a545a 100644 --- a/packages/jest-cli/src/pre_run_message.js +++ b/packages/jest-cli/src/pre_run_message.js @@ -7,19 +7,18 @@ * @flow */ -import {clearLine} from 'jest-util'; +import {clearLine, isInteractive} from 'jest-util'; import chalk from 'chalk'; -import isCI from 'is-ci'; export const print = (stream: stream$Writable | tty$WriteStream) => { - if (process.stdout.isTTY && !isCI) { + if (isInteractive) { stream.write(chalk.bold.dim('Determining test suites to run...')); } }; export const remove = (stream: stream$Writable | tty$WriteStream) => { - if (stream.isTTY && !isCI) { + if (isInteractive) { clearLine(stream); } }; diff --git a/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js b/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js index b6d4c7f0bad0..df1d70b83122 100644 --- a/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js +++ b/packages/jest-cli/src/reporters/__tests__/default_reporter.test.js @@ -38,7 +38,9 @@ beforeEach(() => { jest.useFakeTimers(); // This is not a CI environment, which removes all output by default. - jest.mock('is-ci', () => false); + jest.unmock('jest-util'); + const util = require('jest-util'); + util.isInteractive = true; oldIsTTY = process.stdin.isTTY; oldStdout = process.stdout.write; diff --git a/packages/jest-cli/src/reporters/coverage_reporter.js b/packages/jest-cli/src/reporters/coverage_reporter.js index a8a587474f1a..c70e902b81ba 100644 --- a/packages/jest-cli/src/reporters/coverage_reporter.js +++ b/packages/jest-cli/src/reporters/coverage_reporter.js @@ -20,10 +20,9 @@ import type {GlobalConfig} from 'types/Config'; import type {Context} from 'types/Context'; import type {Test} from 'types/TestRunner'; -import {clearLine} from 'jest-util'; +import {clearLine, isInteractive} from 'jest-util'; import {createReporter} from 'istanbul-api'; import chalk from 'chalk'; -import isCI from 'is-ci'; import istanbulCoverage from 'istanbul-lib-coverage'; import libSourceMaps from 'istanbul-lib-source-maps'; import Worker from 'jest-worker'; @@ -34,8 +33,6 @@ import glob from 'glob'; const FAIL_COLOR = chalk.bold.red; const RUNNING_TEST_COLOR = chalk.bold.dim; -const isInteractive = process.stdout.isTTY && !isCI; - type CoverageWorker = {worker: worker}; export default class CoverageReporter extends BaseReporter { diff --git a/packages/jest-cli/src/reporters/default_reporter.js b/packages/jest-cli/src/reporters/default_reporter.js index a0ea3b306a60..ff1533538c5e 100644 --- a/packages/jest-cli/src/reporters/default_reporter.js +++ b/packages/jest-cli/src/reporters/default_reporter.js @@ -14,9 +14,8 @@ import type {GlobalConfig, Path, ProjectConfig} from 'types/Config'; import type {Test} from 'types/TestRunner'; import type {ReporterOnStartOptions} from 'types/Reporters'; -import {clearLine, getConsoleOutput} from 'jest-util'; +import {clearLine, getConsoleOutput, isInteractive} from 'jest-util'; import chalk from 'chalk'; -import isCI from 'is-ci'; import BaseReporter from './base_reporter'; import Status from './Status'; import getResultHeader from './get_result_header'; @@ -27,8 +26,6 @@ type FlushBufferedOutput = () => void; const TITLE_BULLET = chalk.bold('\u25cf '); -const isInteractive = process.stdin.isTTY && !isCI; - export default class DefaultReporter extends BaseReporter { _clear: string; // ANSI clear sequence for the last printed status _err: write; diff --git a/packages/jest-cli/src/watch.js b/packages/jest-cli/src/watch.js index f6ddfbe50219..df86bfaa0fa9 100644 --- a/packages/jest-cli/src/watch.js +++ b/packages/jest-cli/src/watch.js @@ -16,8 +16,8 @@ import chalk from 'chalk'; import getChangedFilesPromise from './get_changed_files_promise'; import {replacePathSepForRegex} from 'jest-regex-util'; import HasteMap from 'jest-haste-map'; -import isCI from 'is-ci'; import isValidPath from './lib/is_valid_path'; +import {isInteractive} from 'jest-util'; import {print as preRunMessagePrint} from './pre_run_message'; import createContext from './lib/create_context'; import runJest from './run_jest'; @@ -30,7 +30,6 @@ import TestNamePatternPrompt from './test_name_pattern_prompt'; import WatchPluginRegistry from './lib/watch_plugin_registry'; import {KEYS, CLEAR} from './constants'; -const isInteractive = process.stdout.isTTY && !isCI; let hasExitListener = false; export default function watch( @@ -129,16 +128,23 @@ export default function watch( // The old instance that was passed to Jest will still be interrupted // and prevent test runs from the previous run. testWatcher = new TestWatcher({isWatchMode: true}); - if (shouldDisplayWatchUsage) { - outputStream.write( - usage(globalConfig, watchPlugins, hasSnapshotFailure), - ); - shouldDisplayWatchUsage = false; // hide Watch Usage after first run - isWatchUsageDisplayed = true; + + // Do not show any Watch Usage related stuff when running in a + // non-interactive environment + if (isInteractive) { + if (shouldDisplayWatchUsage) { + outputStream.write( + usage(globalConfig, watchPlugins, hasSnapshotFailure), + ); + shouldDisplayWatchUsage = false; // hide Watch Usage after first run + isWatchUsageDisplayed = true; + } else { + outputStream.write(showToggleUsagePrompt()); + shouldDisplayWatchUsage = false; + isWatchUsageDisplayed = false; + } } else { - outputStream.write(showToggleUsagePrompt()); - shouldDisplayWatchUsage = false; - isWatchUsageDisplayed = false; + outputStream.write('\n'); } testNamePatternPrompt.updateCachedTestResults(results.testResults); diff --git a/packages/jest-util/package.json b/packages/jest-util/package.json index 204112225adf..c6e19b6da306 100644 --- a/packages/jest-util/package.json +++ b/packages/jest-util/package.json @@ -13,6 +13,7 @@ "graceful-fs": "^4.1.11", "jest-message-util": "^21.2.1", "jest-validate": "^21.2.1", + "is-ci": "^1.0.10", "mkdirp": "^0.5.1" }, "devDependencies": { diff --git a/packages/jest-util/src/__tests__/is_interactive.test.js b/packages/jest-util/src/__tests__/is_interactive.test.js new file mode 100644 index 000000000000..7cf2a9a8b9a7 --- /dev/null +++ b/packages/jest-util/src/__tests__/is_interactive.test.js @@ -0,0 +1,43 @@ +let oldIsTTY; + +beforeEach(() => { + oldIsTTY = process.stdout.isTTY; +}); + +afterEach(() => { + process.stdout.isTTY = oldIsTTY; + jest.resetModules(); +}); + +it('Returns true when running on interactive environment', () => { + jest.doMock('is-ci', () => false); + process.stdout.isTTY = true; + + const isInteractive = require('../is_interative').default; + expect(isInteractive).toBe(true); +}); + +it('Returns false when running on a non-interactive environment', () => { + let isInteractive; + const expectedResult = false; + + // Test with is-ci being true and isTTY false + jest.doMock('is-ci', () => true); + process.stdout.isTTY = false; + isInteractive = require('../is_interative').default; + expect(isInteractive).toBe(expectedResult); + + // Test with is-ci being false and isTTY false + jest.resetModules(); + jest.doMock('is-ci', () => false); + process.stdout.isTTY = false; + isInteractive = require('../is_interative').default; + expect(isInteractive).toBe(expectedResult); + + // Test with is-ci being true and isTTY true + jest.resetModules(); + jest.doMock('is-ci', () => true); + process.stdout.isTTY = true; + isInteractive = require('../is_interative').default; + expect(isInteractive).toBe(expectedResult); +}); diff --git a/packages/jest-util/src/index.js b/packages/jest-util/src/index.js index 528cb81a626b..a8bc484a6705 100644 --- a/packages/jest-util/src/index.js +++ b/packages/jest-util/src/index.js @@ -18,6 +18,7 @@ import formatTestResults from './format_test_results'; import getConsoleOutput from './get_console_output'; import installCommonGlobals from './install_common_globals'; import NullConsole from './null_console'; +import isInteractive from './is_interative'; import setGlobal from './set_global'; import validateCLIOptions from './validate_cli_options'; @@ -50,6 +51,7 @@ module.exports = { formatTestResults, getConsoleOutput, installCommonGlobals, + isInteractive, realpath, setGlobal, validateCLIOptions, diff --git a/packages/jest-util/src/is_interative.js b/packages/jest-util/src/is_interative.js new file mode 100644 index 000000000000..0ef48457b22c --- /dev/null +++ b/packages/jest-util/src/is_interative.js @@ -0,0 +1,3 @@ +import isCI from 'is-ci'; + +export default process.stdout.isTTY && !isCI;