Skip to content

Commit

Permalink
Avoid writing to stdout in default reporter if --json is enabled. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mjesun authored and cpojer committed Jun 30, 2017
1 parent 5a509c7 commit 68d747d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
78 changes: 78 additions & 0 deletions packages/jest-cli/src/reporters/__tests__/default_reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const aggregatedResults = {};
const options = {};
const testCase = {
context: {config: {rootDir: '/'}},
duration: 0,
path: '/foo',
};
const testResult = {
testFilePath: '/foo',
};

let DefaultReporter;
let stdout;
let stderr;

let oldIsTTY;
let oldStderr;
let oldStdout;

beforeEach(() => {
jest.resetModules();
jest.useFakeTimers();

oldIsTTY = process.stdin.isTTY;
oldStdout = process.stdout.write;
oldStderr = process.stderr.write;

// We mock stderr (even if we do not use it right now on the tests) to avoid
// fake reporters created while testing to mess with the real output of the
// tests itself.
process.stdin.isTTY = true;
stdout = process.stdout.write = jest.fn();
stderr = process.stderr.write = jest.fn();

DefaultReporter = require('../default_reporter');
});

afterEach(() => {
process.stdin.isTTY = oldIsTTY;
process.stdout.write = oldStdout;
process.stderr.write = oldStderr;
});

test('normal output, everything goes to stdout', () => {
const reporter = new DefaultReporter({});

reporter.onRunStart(aggregatedResults, options);
reporter.onTestStart(testCase);
reporter.onTestResult(testCase, testResult, aggregatedResults);;
reporter.onRunComplete();

jest.runAllTimers();

expect(stdout).toHaveBeenCalled();
});

test('when using stderr as output, no stdout call is made', () => {
const reporter = new DefaultReporter({useStderr: true});

reporter.onRunStart(aggregatedResults, options);
reporter.onTestStart(testCase);
reporter.onTestResult(testCase, testResult, aggregatedResults);;
reporter.onRunComplete();

jest.runAllTimers();

expect(stdout).not.toHaveBeenCalled();
});
18 changes: 15 additions & 3 deletions packages/jest-cli/src/reporters/default_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ class DefaultReporter extends BaseReporter {
const flushBufferedOutput = () => {
const string = buffer.join('');
buffer = [];

// This is to avoid conflicts between random output and status text
this._clearStatus();
originalWrite.call(stream, string);
if (string) {
originalWrite.call(stream, string);
}
this._printStatus();

this._bufferedOutput.delete(flushBufferedOutput);
};

Expand Down Expand Up @@ -105,15 +109,23 @@ class DefaultReporter extends BaseReporter {

_clearStatus() {
if (isInteractive) {
this._out(this._clear);
if (this._globalConfig.useStderr) {
this._err(this._clear);
} else {
this._out(this._clear);
}
}
}

_printStatus() {
const {content, clear} = this._status.get();
this._clear = clear;
if (isInteractive) {
this._out(content);
if (this._globalConfig.useStderr) {
this._err(content);
} else {
this._out(content);
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions packages/jest-resolve/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
# yarn lockfile v1


ansi-regex@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df"

ansi-styles@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"

browser-resolve@^1.11.2:
version "1.11.2"
resolved "https://registry.yarnpkg.com/browser-resolve/-/browser-resolve-1.11.2.tgz#8ff09b0a2c421718a1051c260b32e48f442938ce"
Expand All @@ -12,6 +20,26 @@ builtin-modules@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"

chalk@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
dependencies:
ansi-styles "^2.2.1"
escape-string-regexp "^1.0.2"
has-ansi "^2.0.0"
strip-ansi "^3.0.0"
supports-color "^2.0.0"

escape-string-regexp@^1.0.2:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

has-ansi@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
dependencies:
ansi-regex "^2.0.0"

is-builtin-module@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe"
Expand All @@ -31,3 +59,13 @@ resolve@^1.3.2:
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5"
dependencies:
path-parse "^1.0.5"

strip-ansi@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf"
dependencies:
ansi-regex "^2.0.0"

supports-color@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
2 changes: 1 addition & 1 deletion types/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import type Runtime from 'jest-runtime';

export type Test = {|
context: Context,
path: Path,
duration: ?number,
path: Path,
|};

export type Reporter = {
Expand Down

0 comments on commit 68d747d

Please sign in to comment.