Skip to content

Commit

Permalink
Ensure that console output is not lost in concurrent reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
valerybugakov committed Jun 22, 2017
1 parent 0ca04c9 commit 463c7e8
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/jest-cli/src/reporters/DefaultReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class DefaultReporter extends BaseReporter {
_globalConfig: GlobalConfig;
_out: write;
_status: Status;
_bufferedIO: Set<Function>;

constructor(globalConfig: GlobalConfig) {
super();
Expand All @@ -43,6 +44,7 @@ class DefaultReporter extends BaseReporter {
this._out = process.stdout.write.bind(process.stdout);
this._err = process.stderr.write.bind(process.stderr);
this._status = new Status();
this._bufferedIO = new Set();
this._wrapStdio(process.stdout);
this._wrapStdio(process.stderr);
this._status.onChange(() => {
Expand All @@ -57,25 +59,28 @@ class DefaultReporter extends BaseReporter {
let buffer = [];
let timeout = null;

const doFlush = () => {
const flushBufferedIO = () => {
const string = buffer.join('');
buffer = [];
// This is to avoid conflicts between random output and status text
this._clearStatus();
originalWrite.call(stream, string);
this._printStatus();
this._bufferedIO.delete(flushBufferedIO);
};

const flush = () => {
this._bufferedIO.add(flushBufferedIO);

const debouncedFlush = () => {
// If the process blows up no errors would be printed.
// There should be a smart way to buffer stderr, but for now
// we just won't buffer it.
if (stream === process.stderr) {
doFlush();
flushBufferedIO();
} else {
if (!timeout) {
timeout = setTimeout(() => {
doFlush();
flushBufferedIO();
timeout = null;
}, 100);
}
Expand All @@ -85,11 +90,17 @@ class DefaultReporter extends BaseReporter {
// $FlowFixMe
stream.write = chunk => {
buffer.push(chunk);
flush();
debouncedFlush();
return true;
};
}

flushDebouncedIO() {
for (const io of this._bufferedIO) {
io();
}
}

_clearStatus() {
if (isInteractive) {
this._out(this._clear);
Expand All @@ -116,6 +127,7 @@ class DefaultReporter extends BaseReporter {
}

onRunComplete() {
this.flushDebouncedIO();
this._status.runFinished();
// $FlowFixMe
process.stdout.write = this._out;
Expand All @@ -129,6 +141,7 @@ class DefaultReporter extends BaseReporter {
testResult: TestResult,
aggregatedResults: AggregatedResult,
) {
this.flushDebouncedIO();
this._status.testFinished(
test.context.config,
testResult,
Expand Down

0 comments on commit 463c7e8

Please sign in to comment.