Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reporters): verbose reporter should not buffer writes #11054

Merged
merged 8 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"editor.rulers": [80],
"files.exclude": {
"**/.git": true,
"**/node_modules": true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do? (I don't use vs code)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, just saw

As a side note hiding all the build output from VScode is a pain, and again falls into the category of things I consider lying to developers. It wasted a bunch of my time because I couldn't tell that typescript watching was not working.

Copy link
Contributor Author

@conartist6 conartist6 Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it just makes the directory invisible. If your IDE is your primary way to see what's going on in the filesystem you might assume the directories really just don't exist, which they do. It's unnecessarily confusing.

"**/build": true
},
"javascript.validate.enable": false,
"jest.pathToJest": "yarn jest --",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `[@jest/types]` Mark deprecated configuration options as `@deprecated` ([#11913](https://github.com/facebook/jest/pull/11913))
- `[jest-cli]` Improve `--help` printout by removing defunct `--browser` option ([#11914](https://github.com/facebook/jest/pull/11914))
- `[jest-haste-map]` Use distinct cache paths for different values of `computeDependencies` ([#11916](https://github.com/facebook/jest/pull/11916))
- `[@jest/reporters]` Do not buffer `console.log`s when using verbose reporter [#11054](https://github.com/facebook/jest/pull/11054)

### Chore & Maintenance

Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/__snapshots__/consoleDebugging.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`console debugging with --verbose 1`] = ``;

exports[`console debugging with --verbose 2`] = `
PASS __tests__/console-debugging.test.js
✓ verbose mode prints console output synchronously
`;

exports[`console debugging with --verbose 3`] = `
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 1 passed, 1 total
Time: <<REPLACED>>
Ran all test suites.
`;
23 changes: 23 additions & 0 deletions e2e/__tests__/consoleDebugging.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {wrap} from 'jest-snapshot-serializer-raw';
import {extractSummary} from '../Utils';
import runJest from '../runJest';

test('console debugging with --verbose', () => {
const {stderr, stdout, exitCode} = runJest('console-debugging', [
'--noStackTrace',
'--no-cache',
]);
const {summary, rest} = extractSummary(stderr);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
});
24 changes: 24 additions & 0 deletions e2e/console-debugging/__tests__/console-debugging.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const stdoutWrite = require('../stdout-spy');

process.stdout.write = jest.fn(process.stdout.write);

test('verbose mode prints console output synchronously', () => {
console.log('test');

expect(stdoutWrite.text).toMatchInlineSnapshot(`
" console.log
test

at Object.log (__tests__/console-debugging.test.js:14:11)

"
`);
});
14 changes: 14 additions & 0 deletions e2e/console-debugging/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

require('./stdout-spy');

module.exports = {
testEnvironment: 'node',
verbose: true,
};
1 change: 1 addition & 0 deletions e2e/console-debugging/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
26 changes: 26 additions & 0 deletions e2e/console-debugging/stdout-spy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const originalStdoutWrite = process.stdout.write.bind(process.stdout);

global.process.__stdoutWriteMock = global.process.__stdoutWriteMock || null;

/*
This is a terrible hack to ensure that we monkeyPath stdoutWrite before
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops I misspelled this

the jest reporter does...
*/
if (!global.process.__stdoutWriteMock) {
global.process.__stdoutWriteMock = (...args) => {
global.process.__stdoutWriteMock.text = args[0];
originalStdoutWrite(...args);
};

process.stdout.write = global.process.__stdoutWriteMock;
}

module.exports = global.process.__stdoutWriteMock;
24 changes: 13 additions & 11 deletions packages/jest-reporters/src/DefaultReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ export default class DefaultReporter extends BaseReporter {
this._err = process.stderr.write.bind(process.stderr);
this._status = new Status();
this._bufferedOutput = new Set();
this._wrapStdio(process.stdout);
this._wrapStdio(process.stderr);
this.__wrapStdio(process.stdout);
this.__wrapStdio(process.stderr);
this._status.onChange(() => {
this._clearStatus();
this._printStatus();
this.__clearStatus();
this.__printStatus();
});
}

private _wrapStdio(stream: NodeJS.WritableStream | NodeJS.WriteStream) {
const originalWrite = stream.write;
protected __wrapStdio(
stream: NodeJS.WritableStream | NodeJS.WriteStream,
): void {
const write = stream.write.bind(stream);

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

// This is to avoid conflicts between random output and status text
this._clearStatus();
this.__clearStatus();
if (string) {
originalWrite.call(stream, string);
write(string);
}
this._printStatus();
this.__printStatus();

this._bufferedOutput.delete(flushBufferedOutput);
};
Expand Down Expand Up @@ -103,7 +105,7 @@ export default class DefaultReporter extends BaseReporter {
}
}

private _clearStatus() {
protected __clearStatus(): void {
if (isInteractive) {
if (this._globalConfig.useStderr) {
this._err(this._clear);
Expand All @@ -113,7 +115,7 @@ export default class DefaultReporter extends BaseReporter {
}
}

private _printStatus() {
protected __printStatus(): void {
const {content, clear} = this._status.get();
this._clear = clear;
if (isInteractive) {
Expand Down
15 changes: 15 additions & 0 deletions packages/jest-reporters/src/VerboseReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ export default class VerboseReporter extends DefaultReporter {
this._globalConfig = globalConfig;
}

// Verbose mode is for debugging. Buffering of output is undesirable.
// See https://github.com/facebook/jest/issues/8208
protected __wrapStdio(
stream: NodeJS.WritableStream | NodeJS.WriteStream,
): void {
const write = stream.write.bind(stream);

stream.write = (chunk: string) => {
this.__clearStatus();
write(chunk);
this.__printStatus();
return true;
};
}

static filterTestResults(
testResults: Array<AssertionResult>,
): Array<AssertionResult> {
Expand Down