Skip to content

Commit 7f881af

Browse files
authored
fix(reporters): verbose reporter should not buffer writes (#11054)
1 parent cdc64c6 commit 7f881af

File tree

12 files changed

+144
-13
lines changed

12 files changed

+144
-13
lines changed

.vscode/settings.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
"editor.rulers": [80],
33
"files.exclude": {
44
"**/.git": true,
5-
"**/node_modules": true,
6-
"**/build": true
75
},
86
"javascript.validate.enable": false,
97
"jest.pathToJest": "yarn jest --",

CHANGELOG.md

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

1617
### Chore & Maintenance
1718

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`console debugging with --verbose 1`] = `
4+
console.log
5+
test
6+
7+
at Object.log (__tests__/console-debugging.test.js:17:11)
8+
9+
`;
10+
11+
exports[`console debugging with --verbose 2`] = `
12+
PASS __tests__/console-debugging.test.js
13+
✓ verbose mode prints console output synchronously
14+
`;
15+
16+
exports[`console debugging with --verbose 3`] = `
17+
Test Suites: 1 passed, 1 total
18+
Tests: 1 passed, 1 total
19+
Snapshots: 1 passed, 1 total
20+
Time: <<REPLACED>>
21+
Ran all test suites.
22+
`;
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
// test only works consistently without tty
15+
expect(process.stdout.isTTY).not.toBe(true);
16+
17+
console.log('test');
18+
19+
expect(stdoutWrite.text).toMatchInlineSnapshot(`
20+
" console.log
21+
test
22+
23+
at Object.log (__tests__/console-debugging.test.js:17:11)
24+
25+
"
26+
`);
27+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
require('./stdout-spy');
10+
11+
module.exports = {
12+
testEnvironment: 'node',
13+
verbose: true,
14+
};

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 originalStdoutWrite = process.stdout.write.bind(process.stdout);
10+
11+
global.process.__stdoutWriteMock = global.process.__stdoutWriteMock || null;
12+
13+
/*
14+
This is a terrible hack to ensure that we monkeyPath stdoutWrite before
15+
the jest reporter does...
16+
*/
17+
if (!global.process.__stdoutWriteMock) {
18+
global.process.__stdoutWriteMock = (...args) => {
19+
global.process.__stdoutWriteMock.text = args[0];
20+
originalStdoutWrite(...args);
21+
};
22+
23+
process.stdout.write = global.process.__stdoutWriteMock;
24+
}
25+
26+
module.exports = global.process.__stdoutWriteMock;

packages/jest-reporters/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@jest/test-result": "^27.2.4",
1515
"@jest/transform": "^27.2.4",
1616
"@jest/types": "^27.2.4",
17+
"@types/node": "*",
1718
"chalk": "^4.0.0",
1819
"collect-v8-coverage": "^1.0.0",
1920
"exit": "^0.1.2",

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) {

0 commit comments

Comments
 (0)