Skip to content

Commit 6413244

Browse files
committed
Ensure verbose mode prints output synchronously
Fixes #8208
1 parent 24f9bc8 commit 6413244

File tree

9 files changed

+117
-11
lines changed

9 files changed

+117
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `[@jest/types]` Mark deprecated configuration options as `@deprecated` ([#11913](https://github.com/facebook/jest/pull/11913))
1111
- `[jest-cli]` Improve `--help` printout by removing defunct `--browser` option ([#11914](https://github.com/facebook/jest/pull/11914))
1212
- `[jest-haste-map]` Use distinct cache paths for different values of `computeDependencies` ([#11916](https://github.com/facebook/jest/pull/11916))
13+
- `[@jest/reporters]` Do not buffer `console.log`s when using verbose reporter [#11054](https://github.com/facebook/jest/pull/11054)
1314

1415
### Chore & Maintenance
1516

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
16+
expect(stdoutWrite.text).toMatchInlineSnapshot(`
17+
" console.log
18+
test
19+
20+
at Object.log (__tests__/console-debugging.test.js:14:11)
21+
22+
"
23+
`);
24+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require('./stdout-spy');
2+
3+
module.exports = {
4+
testEnvironment: 'node',
5+
verbose: true,
6+
};

e2e/console-debugging/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const originalStdoutWrite = process.stdout.write;
2+
3+
global.process.__stdoutWriteMock = global.process.__stdoutWriteMock || null;
4+
5+
/*
6+
This is a terrible hack to ensure that we monkeyPath stdoutWrite before
7+
the jest reporter does...
8+
*/
9+
if (!global.process.__stdoutWriteMock) {
10+
global.process.__stdoutWriteMock = (...args) => {
11+
global.process.__stdoutWriteMock.text = args[0];
12+
originalStdoutWrite(...args);
13+
};
14+
15+
process.stdout.write = global.process.__stdoutWriteMock;
16+
}
17+
18+
module.exports = global.process.__stdoutWriteMock;

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)