Skip to content

Commit f29c03e

Browse files
ranyitzcpojer
authored andcommitted
add methods to custom and buffered consoles (#5514)
* add methods to custom and buffered consoles * update changelog with the console methods addition * use spyOn console to mock the implementation * refactor console's calls to use ...args instead of arguments
1 parent 09adc31 commit f29c03e

File tree

6 files changed

+493
-26
lines changed

6 files changed

+493
-26
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## master
22

3+
### Features
4+
5+
* `[jest-util]` Add the following methods to the "console" implementations:
6+
`assert`, `count`, `countReset`, `dir`, `dirxml`, `group`, `groupCollapsed`,
7+
`groupEnd`, `time`, `timeEnd`
8+
([#5514](https://github.com/facebook/jest/pull/5514))
9+
310
## jest 22.2.2
411

512
### Fixes
@@ -16,8 +23,9 @@
1623
([#5494](https://github.com/facebook/jest/pull/5494))
1724

1825
### Chore & Maintenance
19-
* `[filenames]` Standardize file names in root ([#5500](https://github.com/facebook/jest/pull/5500))
2026

27+
* `[filenames]` Standardize file names in root
28+
([#5500](https://github.com/facebook/jest/pull/5500))
2129

2230
## jest 22.2.1
2331

packages/jest-util/src/Console.js

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
*/
99
/* global stream$Writable */
1010

11-
import type {LogType, LogMessage} from 'types/Console';
11+
import type {LogType, LogMessage, LogCounters, LogTimers} from 'types/Console';
1212

1313
import {format} from 'util';
1414
import {Console} from 'console';
15+
import chalk from 'chalk';
1516
import clearLine from './clear_line';
1617

1718
type Formatter = (type: LogType, message: LogMessage) => string;
1819

1920
export default class CustomConsole extends Console {
2021
_stdout: stream$Writable;
2122
_formatBuffer: Formatter;
23+
_counters: LogCounters;
24+
_timers: LogTimers;
25+
_groupDepth: number;
2226

2327
constructor(
2428
stdout: stream$Writable,
@@ -27,31 +31,107 @@ export default class CustomConsole extends Console {
2731
) {
2832
super(stdout, stderr);
2933
this._formatBuffer = formatBuffer || ((type, message) => message);
34+
this._counters = {};
35+
this._timers = {};
36+
this._groupDepth = 0;
37+
}
38+
39+
_logToParentConsole(message: string) {
40+
super.log(message);
3041
}
3142

3243
_log(type: LogType, message: string) {
3344
clearLine(this._stdout);
34-
super.log(this._formatBuffer(type, message));
45+
this._logToParentConsole(
46+
this._formatBuffer(type, ' '.repeat(this._groupDepth) + message),
47+
);
48+
}
49+
50+
assert(...args: Array<any>) {
51+
if (args[0]) {
52+
this._log('assert', format(...args.slice(1)));
53+
}
54+
}
55+
56+
count(label: string = 'default') {
57+
if (!this._counters[label]) {
58+
this._counters[label] = 0;
59+
}
60+
61+
this._log('count', format(`${label}: ${++this._counters[label]}`));
3562
}
3663

37-
debug(...args: Array<mixed>) {
38-
this._log('debug', format.apply(null, arguments));
64+
countReset(label: string = 'default') {
65+
this._counters[label] = 0;
3966
}
4067

41-
log(...args: Array<mixed>) {
42-
this._log('log', format.apply(null, arguments));
68+
debug(...args: Array<any>) {
69+
this._log('debug', format(...args));
4370
}
4471

45-
info(...args: Array<mixed>) {
46-
this._log('info', format.apply(null, arguments));
72+
dir(...args: Array<any>) {
73+
this._log('dir', format(...args));
74+
}
75+
76+
dirxml(...args: Array<any>) {
77+
this._log('dirxml', format(...args));
78+
}
79+
80+
error(...args: Array<any>) {
81+
this._log('error', format(...args));
82+
}
83+
84+
group(...args: Array<any>) {
85+
this._groupDepth++;
86+
87+
if (args.length > 0) {
88+
this._log('group', chalk.bold(format(...args)));
89+
}
4790
}
4891

49-
warn(...args: Array<mixed>) {
50-
this._log('warn', format.apply(null, arguments));
92+
groupCollapsed(...args: Array<any>) {
93+
this._groupDepth++;
94+
95+
if (args.length > 0) {
96+
this._log('groupCollapsed', chalk.bold(format(...args)));
97+
}
98+
}
99+
100+
groupEnd() {
101+
if (this._groupDepth > 0) {
102+
this._groupDepth--;
103+
}
104+
}
105+
106+
info(...args: Array<any>) {
107+
this._log('info', format(...args));
108+
}
109+
110+
log(...args: Array<any>) {
111+
this._log('log', format(...args));
112+
}
113+
114+
time(label: string = 'default') {
115+
if (this._timers[label]) {
116+
return;
117+
}
118+
119+
this._timers[label] = new Date();
120+
}
121+
122+
timeEnd(label: string = 'default') {
123+
const startTime = this._timers[label];
124+
125+
if (startTime) {
126+
const endTime = new Date();
127+
const time = (endTime - startTime) / 1000;
128+
this._log('time', format(`${label}: ${time}ms`));
129+
delete this._timers[label];
130+
}
51131
}
52132

53-
error(...args: Array<mixed>) {
54-
this._log('error', format.apply(null, arguments));
133+
warn(...args: Array<any>) {
134+
this._log('warn', format(...args));
55135
}
56136

57137
getBuffer() {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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+
* @flow
8+
*/
9+
10+
import chalk from 'chalk';
11+
import BufferedConsole from '../buffered_console';
12+
13+
describe('CustomConsole', () => {
14+
let _console;
15+
const stdout = () =>
16+
_console
17+
.getBuffer()
18+
.map(log => log.message)
19+
.join('\n');
20+
21+
beforeEach(() => {
22+
_console = new BufferedConsole();
23+
});
24+
25+
describe('assert', () => {
26+
test('log when the assertion is truthy', () => {
27+
_console.assert(true, 'ok');
28+
29+
expect(stdout()).toMatch('ok');
30+
});
31+
32+
test('do not log when the assertion is falsy', () => {
33+
_console.assert(false, 'ok');
34+
35+
expect(stdout()).toEqual('');
36+
});
37+
});
38+
39+
describe('count', () => {
40+
test('count using the default counter', () => {
41+
_console.count();
42+
_console.count();
43+
_console.count();
44+
45+
expect(stdout()).toEqual('default: 1\ndefault: 2\ndefault: 3');
46+
});
47+
48+
test('count using the a labeled counter', () => {
49+
_console.count('custom');
50+
_console.count('custom');
51+
_console.count('custom');
52+
53+
expect(stdout()).toEqual('custom: 1\ncustom: 2\ncustom: 3');
54+
});
55+
56+
test('countReset restarts default counter', () => {
57+
_console.count();
58+
_console.count();
59+
_console.countReset();
60+
_console.count();
61+
expect(stdout()).toEqual('default: 1\ndefault: 2\ndefault: 1');
62+
});
63+
64+
test('countReset restarts custom counter', () => {
65+
_console.count('custom');
66+
_console.count('custom');
67+
_console.countReset('custom');
68+
_console.count('custom');
69+
70+
expect(stdout()).toEqual('custom: 1\ncustom: 2\ncustom: 1');
71+
});
72+
});
73+
74+
describe('group', () => {
75+
test('group without label', () => {
76+
_console.group();
77+
_console.log('hey');
78+
_console.group();
79+
_console.log('there');
80+
81+
expect(stdout()).toEqual(' hey\n there');
82+
});
83+
84+
test('group with label', () => {
85+
_console.group('first');
86+
_console.log('hey');
87+
_console.group('second');
88+
_console.log('there');
89+
90+
expect(stdout()).toEqual(` ${chalk.bold('first')}
91+
hey
92+
${chalk.bold('second')}
93+
there`);
94+
});
95+
96+
test('groupEnd remove the indentation of the current group', () => {
97+
_console.group();
98+
_console.log('hey');
99+
_console.groupEnd();
100+
_console.log('there');
101+
102+
expect(stdout()).toEqual(' hey\nthere');
103+
});
104+
105+
test('groupEnd can not remove the indentation below the starting point', () => {
106+
_console.groupEnd();
107+
_console.groupEnd();
108+
_console.group();
109+
_console.log('hey');
110+
_console.groupEnd();
111+
_console.log('there');
112+
113+
expect(stdout()).toEqual(' hey\nthere');
114+
});
115+
});
116+
117+
describe('time', () => {
118+
test('should return the time between time() and timeEnd() on default timer', () => {
119+
_console.time();
120+
_console.timeEnd();
121+
122+
expect(stdout()).toMatch('default: ');
123+
expect(stdout()).toMatch('ms');
124+
});
125+
126+
test('should return the time between time() and timeEnd() on custom timer', () => {
127+
_console.time('custom');
128+
_console.timeEnd('custom');
129+
130+
expect(stdout()).toMatch('custom: ');
131+
expect(stdout()).toMatch('ms');
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)