Skip to content

Commit

Permalink
cli: Hide Watch Usage output when running on non-interactive envs (#4958
Browse files Browse the repository at this point in the history
)

* cli: Hide Watch Usage output when running on non-interactive environments

* cli: Hide no test found related messages on non-interactive environments

* Move isInteractive to jest-util

* Use isInteractive in other parts of the code

* Fix tests that depend on interactive environment

* Fix tests in AppVeyor and add tests to the is_interactive module

* Update CHANGELOG for #4958
  • Loading branch information
taylon authored and cpojer committed Dec 7, 2017
1 parent 357a119 commit d695ab3
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 [
"
Expand Down
31 changes: 29 additions & 2 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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],
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 3 additions & 4 deletions packages/jest-cli/src/pre_run_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-cli/src/reporters/coverage_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-cli/src/reporters/default_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
28 changes: 17 additions & 11 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
43 changes: 43 additions & 0 deletions packages/jest-util/src/__tests__/is_interactive.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
2 changes: 2 additions & 0 deletions packages/jest-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -50,6 +51,7 @@ module.exports = {
formatTestResults,
getConsoleOutput,
installCommonGlobals,
isInteractive,
realpath,
setGlobal,
validateCLIOptions,
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-util/src/is_interative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import isCI from 'is-ci';

export default process.stdout.isTTY && !isCI;

0 comments on commit d695ab3

Please sign in to comment.