Skip to content

Commit 463c7e8

Browse files
committed
Ensure that console output is not lost in concurrent reporter
1 parent 0ca04c9 commit 463c7e8

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

packages/jest-cli/src/reporters/DefaultReporter.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DefaultReporter extends BaseReporter {
3535
_globalConfig: GlobalConfig;
3636
_out: write;
3737
_status: Status;
38+
_bufferedIO: Set<Function>;
3839

3940
constructor(globalConfig: GlobalConfig) {
4041
super();
@@ -43,6 +44,7 @@ class DefaultReporter extends BaseReporter {
4344
this._out = process.stdout.write.bind(process.stdout);
4445
this._err = process.stderr.write.bind(process.stderr);
4546
this._status = new Status();
47+
this._bufferedIO = new Set();
4648
this._wrapStdio(process.stdout);
4749
this._wrapStdio(process.stderr);
4850
this._status.onChange(() => {
@@ -57,25 +59,28 @@ class DefaultReporter extends BaseReporter {
5759
let buffer = [];
5860
let timeout = null;
5961

60-
const doFlush = () => {
62+
const flushBufferedIO = () => {
6163
const string = buffer.join('');
6264
buffer = [];
6365
// This is to avoid conflicts between random output and status text
6466
this._clearStatus();
6567
originalWrite.call(stream, string);
6668
this._printStatus();
69+
this._bufferedIO.delete(flushBufferedIO);
6770
};
6871

69-
const flush = () => {
72+
this._bufferedIO.add(flushBufferedIO);
73+
74+
const debouncedFlush = () => {
7075
// If the process blows up no errors would be printed.
7176
// There should be a smart way to buffer stderr, but for now
7277
// we just won't buffer it.
7378
if (stream === process.stderr) {
74-
doFlush();
79+
flushBufferedIO();
7580
} else {
7681
if (!timeout) {
7782
timeout = setTimeout(() => {
78-
doFlush();
83+
flushBufferedIO();
7984
timeout = null;
8085
}, 100);
8186
}
@@ -85,11 +90,17 @@ class DefaultReporter extends BaseReporter {
8590
// $FlowFixMe
8691
stream.write = chunk => {
8792
buffer.push(chunk);
88-
flush();
93+
debouncedFlush();
8994
return true;
9095
};
9196
}
9297

98+
flushDebouncedIO() {
99+
for (const io of this._bufferedIO) {
100+
io();
101+
}
102+
}
103+
93104
_clearStatus() {
94105
if (isInteractive) {
95106
this._out(this._clear);
@@ -116,6 +127,7 @@ class DefaultReporter extends BaseReporter {
116127
}
117128

118129
onRunComplete() {
130+
this.flushDebouncedIO();
119131
this._status.runFinished();
120132
// $FlowFixMe
121133
process.stdout.write = this._out;
@@ -129,6 +141,7 @@ class DefaultReporter extends BaseReporter {
129141
testResult: TestResult,
130142
aggregatedResults: AggregatedResult,
131143
) {
144+
this.flushDebouncedIO();
132145
this._status.testFinished(
133146
test.context.config,
134147
testResult,

0 commit comments

Comments
 (0)