Skip to content

Commit 6203ed6

Browse files
committed
CustomConsole: print console.error and console.assert to stderr
1 parent 96c19a6 commit 6203ed6

3 files changed

Lines changed: 58 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
### Fixes
99

10+
- `[@jest/console]` Print to stderr when calling `console.error` or `console.assert` in verbose mode or using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
1011
- `[expect]` Add negative equality tests for iterables ([#8260](https://github.com/facebook/jest/pull/8260))
1112
- `[jest-haste-map]` Resolve fs watcher EMFILE error ([#8258](https://github.com/facebook/jest/pull/8258))
1213

packages/jest-console/src/CustomConsole.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import chalk from 'chalk';
1212
import {LogCounters, LogMessage, LogTimers, LogType} from './types';
1313

1414
// TODO: Copied from `jest-util`. Import from it in Jest 25
15-
function clearLine(stream: NodeJS.WritableStream) {
16-
if (process.stdout.isTTY) {
15+
function clearLine(stream: NodeJS.WritableStream & {isTTY?: boolean}) {
16+
if (stream.isTTY) {
1717
stream.write('\x1b[999D\x1b[K');
1818
}
1919
}
@@ -22,6 +22,7 @@ type Formatter = (type: LogType, message: LogMessage) => string;
2222

2323
export default class CustomConsole extends Console {
2424
private _stdout: NodeJS.WritableStream;
25+
private _stderr: NodeJS.WritableStream;
2526
private _formatBuffer: Formatter;
2627
private _counters: LogCounters;
2728
private _timers: LogTimers;
@@ -34,21 +35,27 @@ export default class CustomConsole extends Console {
3435
) {
3536
super(stdout, stderr);
3637
this._stdout = stdout;
38+
this._stderr = stderr;
3739
this._formatBuffer = formatBuffer;
3840
this._counters = {};
3941
this._timers = {};
4042
this._groupDepth = 0;
4143
}
4244

43-
private _logToParentConsole(message: string) {
44-
super.log(message);
45-
}
46-
4745
private _log(type: LogType, message: string) {
48-
clearLine(this._stdout);
49-
this._logToParentConsole(
50-
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
46+
const isErrorType = type === 'error' || type === 'assert';
47+
const formattedMessage = this._formatBuffer(
48+
type,
49+
' '.repeat(this._groupDepth) + message,
5150
);
51+
52+
if (isErrorType) {
53+
clearLine(this._stderr);
54+
super.error(formattedMessage);
55+
} else {
56+
clearLine(this._stdout);
57+
super.log(formattedMessage);
58+
}
5259
}
5360

5461
assert(value: any, message?: string | Error) {

packages/jest-console/src/__tests__/console.test.ts renamed to packages/jest-console/src/__tests__/CustomConsole.test.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,77 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {Writable} from 'stream';
89
import chalk from 'chalk';
910
import CustomConsole from '../CustomConsole';
1011

1112
describe('CustomConsole', () => {
1213
let _console;
13-
let _stdout = '';
14+
let _stdout;
15+
let _stderr;
1416

1517
beforeEach(() => {
16-
_console = new CustomConsole(process.stdout, process.stderr);
17-
jest.spyOn(_console, '_logToParentConsole').mockImplementation(message => {
18-
_stdout += message + '\n';
18+
_stdout = '';
19+
_stderr = '';
20+
21+
const stdout = new Writable({
22+
write(chunk, encoding, callback) {
23+
_stdout += chunk.toString();
24+
callback();
25+
},
1926
});
2027

21-
_stdout = '';
28+
const stderr = new Writable({
29+
write(chunk, encoding, callback) {
30+
_stderr += chunk.toString();
31+
callback();
32+
},
33+
});
34+
35+
_console = new CustomConsole(stdout, stderr);
36+
});
37+
38+
describe('log', () => {
39+
test('should print to stdout', () => {
40+
_console.log('Hello world!');
41+
42+
expect(_stdout).toBe('Hello world!\n');
43+
});
44+
});
45+
46+
describe('error', () => {
47+
test('should print to stderr', () => {
48+
_console.error('Found some error!');
49+
50+
expect(_stderr).toBe('Found some error!\n');
51+
});
2252
});
2353

2454
describe('assert', () => {
2555
test('do not log when the assertion is truthy', () => {
2656
_console.assert(true);
2757

28-
expect(_stdout).toMatch('');
58+
expect(_stderr).toMatch('');
2959
});
3060

3161
test('do not log when the assertion is truthy and there is a message', () => {
3262
_console.assert(true, 'ok');
3363

34-
expect(_stdout).toMatch('');
64+
expect(_stderr).toMatch('');
3565
});
3666

3767
test('log the assertion error when the assertion is falsy', () => {
3868
_console.assert(false);
3969

40-
expect(_stdout).toMatch('AssertionError');
41-
expect(_stdout).toMatch('false == true');
70+
expect(_stderr).toMatch('AssertionError');
71+
expect(_stderr).toMatch('false == true');
4272
});
4373

4474
test('log the assertion error when the assertion is falsy with another message argument', () => {
4575
_console.assert(false, 'ok');
4676

47-
expect(_stdout).toMatch('AssertionError');
48-
expect(_stdout).toMatch('ok');
77+
expect(_stderr).toMatch('AssertionError');
78+
expect(_stderr).toMatch('ok');
4979
});
5080
});
5181

0 commit comments

Comments
 (0)