Skip to content

Commit 02de105

Browse files
committed
Ensure verbose mode prints output synchronously
Fixes #8208
1 parent 98ed700 commit 02de105

File tree

7 files changed

+109
-11
lines changed

7 files changed

+109
-11
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`console debugging with --verbose 1`] = ``;
4+
5+
exports[`console debugging with --verbose 2`] = `
6+
PASS __tests__/console-debugging.test.js
7+
✓ verbose mode prints console output synchronously
8+
`;
9+
10+
exports[`console debugging with --verbose 3`] = `
11+
Test Suites: 1 passed, 1 total
12+
Tests: 1 passed, 1 total
13+
Snapshots: 1 passed, 1 total
14+
Time: <<REPLACED>>
15+
Ran all test suites.
16+
`;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {wrap} from 'jest-snapshot-serializer-raw';
9+
import {extractSummary} from '../Utils';
10+
import runJest from '../runJest';
11+
12+
test('console debugging with --verbose', () => {
13+
const {stderr, stdout, exitCode} = runJest('console-debugging', [
14+
'--noStackTrace',
15+
'--no-cache',
16+
]);
17+
const {summary, rest} = extractSummary(stderr);
18+
19+
expect(exitCode).toBe(0);
20+
expect(wrap(stdout)).toMatchSnapshot();
21+
expect(wrap(rest)).toMatchSnapshot();
22+
expect(wrap(summary)).toMatchSnapshot();
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const stdoutWrite = require('../stdout-spy');
10+
11+
process.stdout.write = jest.fn(process.stdout.write);
12+
13+
test('verbose mode prints console output synchronously', () => {
14+
console.log('test');
15+
expect(stdoutWrite.text).toMatchInlineSnapshot(`
16+
" console.log
17+
test
18+
19+
at Object.log (__tests__/console-debugging.test.js:14:11)
20+
21+
"
22+
`);
23+
});

e2e/console-debugging/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node",
4+
"verbose": true,
5+
"setupFiles": [
6+
"./stdout-spy.js"
7+
]
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const originalStdoutWrite = process.stdout.write;
2+
3+
const stdoutWrite = (...args) => {
4+
stdoutWrite.text = args[0];
5+
originalStdoutWrite(...args);
6+
};
7+
8+
process.stdout.write = stdoutWrite;
9+
10+
module.exports = stdoutWrite;

packages/jest-reporters/src/DefaultReporter.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ export default class DefaultReporter extends BaseReporter {
4343
this._err = process.stderr.write.bind(process.stderr);
4444
this._status = new Status();
4545
this._bufferedOutput = new Set();
46-
this._wrapStdio(process.stdout);
47-
this._wrapStdio(process.stderr);
46+
this.__wrapStdio(process.stdout);
47+
this.__wrapStdio(process.stderr);
4848
this._status.onChange(() => {
49-
this._clearStatus();
50-
this._printStatus();
49+
this.__clearStatus();
50+
this.__printStatus();
5151
});
5252
}
5353

54-
private _wrapStdio(stream: NodeJS.WritableStream | NodeJS.WriteStream) {
55-
const originalWrite = stream.write;
54+
protected __wrapStdio(
55+
stream: NodeJS.WritableStream | NodeJS.WriteStream,
56+
): void {
57+
const write = stream.write.bind(stream);
5658

5759
let buffer: Array<string> = [];
5860
let timeout: NodeJS.Timeout | null = null;
@@ -62,11 +64,11 @@ export default class DefaultReporter extends BaseReporter {
6264
buffer = [];
6365

6466
// This is to avoid conflicts between random output and status text
65-
this._clearStatus();
67+
this.__clearStatus();
6668
if (string) {
67-
originalWrite.call(stream, string);
69+
write(string);
6870
}
69-
this._printStatus();
71+
this.__printStatus();
7072

7173
this._bufferedOutput.delete(flushBufferedOutput);
7274
};
@@ -103,7 +105,7 @@ export default class DefaultReporter extends BaseReporter {
103105
}
104106
}
105107

106-
private _clearStatus() {
108+
protected __clearStatus(): void {
107109
if (isInteractive) {
108110
if (this._globalConfig.useStderr) {
109111
this._err(this._clear);
@@ -113,7 +115,7 @@ export default class DefaultReporter extends BaseReporter {
113115
}
114116
}
115117

116-
private _printStatus() {
118+
protected __printStatus(): void {
117119
const {content, clear} = this._status.get();
118120
this._clear = clear;
119121
if (isInteractive) {

packages/jest-reporters/src/VerboseReporter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ export default class VerboseReporter extends DefaultReporter {
2929
this._globalConfig = globalConfig;
3030
}
3131

32+
// Verbose mode is for debugging. Buffering of output is undesirable.
33+
// See https://github.com/facebook/jest/issues/8208
34+
protected __wrapStdio(
35+
stream: NodeJS.WritableStream | NodeJS.WriteStream,
36+
): void {
37+
const write = stream.write.bind(stream);
38+
39+
stream.write = (chunk: string) => {
40+
this.__clearStatus();
41+
write(chunk);
42+
this.__printStatus();
43+
return true;
44+
};
45+
}
46+
3247
static filterTestResults(
3348
testResults: Array<AssertionResult>,
3449
): Array<AssertionResult> {

0 commit comments

Comments
 (0)