Skip to content

Commit 3e92cb1

Browse files
authored
CustomConsole: print console.error and console.assert to stderr (#8261)
* CustomConsole: print console.error and console.assert to stderr * Print to stderr when using console.warn * Fix difference between node versions
1 parent c85a5e9 commit 3e92cb1

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[@jest/console]` Print to stderr when calling `console.error`, `console.warn` or `console.assert` using the `jest-runtime` CLI ([#8261](https://github.com/facebook/jest/pull/8261))
8+
79
### Chore & Maintenance
810

911
### Performance

packages/jest-console/src/CustomConsole.ts

+15-10
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,19 +35,23 @@ 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) {
4846
clearLine(this._stdout);
49-
this._logToParentConsole(
47+
super.log(
48+
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
49+
);
50+
}
51+
52+
private _logError(type: LogType, message: string) {
53+
clearLine(this._stderr);
54+
super.error(
5055
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
5156
);
5257
}
@@ -55,7 +60,7 @@ export default class CustomConsole extends Console {
5560
try {
5661
assert(value, message);
5762
} catch (error) {
58-
this._log('assert', error.toString());
63+
this._logError('assert', error.toString());
5964
}
6065
}
6166

@@ -84,7 +89,7 @@ export default class CustomConsole extends Console {
8489
}
8590

8691
error(firstArg: any, ...args: Array<any>) {
87-
this._log('error', format(firstArg, ...args));
92+
this._logError('error', format(firstArg, ...args));
8893
}
8994

9095
group(title?: string, ...args: Array<any>) {
@@ -137,7 +142,7 @@ export default class CustomConsole extends Console {
137142
}
138143

139144
warn(firstArg: any, ...args: Array<any>) {
140-
this._log('warn', format(firstArg, ...args));
145+
this._logError('warn', format(firstArg, ...args));
141146
}
142147

143148
getBuffer() {

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

+53-12
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,88 @@
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+
});
52+
});
53+
54+
describe('warn', () => {
55+
test('should print to stderr', () => {
56+
_console.warn('Found some warning!');
57+
58+
expect(_stderr).toBe('Found some warning!\n');
59+
});
2260
});
2361

2462
describe('assert', () => {
2563
test('do not log when the assertion is truthy', () => {
2664
_console.assert(true);
2765

28-
expect(_stdout).toMatch('');
66+
expect(_stderr).toMatch('');
2967
});
3068

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

34-
expect(_stdout).toMatch('');
72+
expect(_stderr).toMatch('');
3573
});
3674

3775
test('log the assertion error when the assertion is falsy', () => {
3876
_console.assert(false);
3977

40-
expect(_stdout).toMatch('AssertionError');
41-
expect(_stdout).toMatch('false == true');
78+
expect(_stderr).toMatch('AssertionError');
79+
expect(_stderr).toMatch(
80+
// The message may differ across Node versions
81+
/(false == true)|(The expression evaluated to a falsy value:)/,
82+
);
4283
});
4384

4485
test('log the assertion error when the assertion is falsy with another message argument', () => {
45-
_console.assert(false, 'ok');
86+
_console.assert(false, 'this should not happen');
4687

47-
expect(_stdout).toMatch('AssertionError');
48-
expect(_stdout).toMatch('ok');
88+
expect(_stderr).toMatch('AssertionError');
89+
expect(_stderr).toMatch('this should not happen');
4990
});
5091
});
5192

packages/jest-runner/src/runTest.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,9 @@ async function runTestInternal(
135135
let testConsole;
136136

137137
if (globalConfig.silent) {
138-
testConsole = new NullConsole(consoleOut, process.stderr, consoleFormatter);
138+
testConsole = new NullConsole(consoleOut, consoleOut, consoleFormatter);
139139
} else if (globalConfig.verbose) {
140-
testConsole = new CustomConsole(
141-
consoleOut,
142-
process.stderr,
143-
consoleFormatter,
144-
);
140+
testConsole = new CustomConsole(consoleOut, consoleOut, consoleFormatter);
145141
} else {
146142
testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
147143
}

0 commit comments

Comments
 (0)