/
bufferedConsole.test.ts
193 lines (153 loc) · 5.01 KB
/
bufferedConsole.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* 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 chalk = require('chalk');
import BufferedConsole from '../BufferedConsole';
describe('CustomConsole', () => {
let _console: BufferedConsole;
const stdout = () => {
const buffer = _console.getBuffer();
if (!buffer) {
return '';
}
return buffer.map(log => log.message).join('\n');
};
beforeEach(() => {
_console = new BufferedConsole(() => null);
});
describe('assert', () => {
test('do not log when the assertion is truthy', () => {
_console.assert(true);
expect(stdout()).toMatch('');
});
test('do not log when the assertion is truthy and there is a message', () => {
_console.assert(true, 'ok');
expect(stdout()).toMatch('');
});
test('log the assertion error when the assertion is falsy', () => {
_console.assert(false);
expect(stdout()).toMatch('AssertionError');
expect(stdout()).toMatch('false == true');
});
test('log the assertion error when the assertion is falsy with another message argument', () => {
_console.assert(false, 'ok');
expect(stdout()).toMatch('AssertionError');
expect(stdout()).toMatch('ok');
});
});
describe('count', () => {
test('count using the default counter', () => {
_console.count();
_console.count();
_console.count();
expect(stdout()).toEqual('default: 1\ndefault: 2\ndefault: 3');
});
test('count using the a labeled counter', () => {
_console.count('custom');
_console.count('custom');
_console.count('custom');
expect(stdout()).toEqual('custom: 1\ncustom: 2\ncustom: 3');
});
test('countReset restarts default counter', () => {
_console.count();
_console.count();
_console.countReset();
_console.count();
expect(stdout()).toEqual('default: 1\ndefault: 2\ndefault: 1');
});
test('countReset restarts custom counter', () => {
_console.count('custom');
_console.count('custom');
_console.countReset('custom');
_console.count('custom');
expect(stdout()).toEqual('custom: 1\ncustom: 2\ncustom: 1');
});
});
describe('group', () => {
test('group without label', () => {
_console.group();
_console.log('hey');
_console.group();
_console.log('there');
expect(stdout()).toEqual(' hey\n there');
});
test('group with label', () => {
_console.group('first');
_console.log('hey');
_console.group('second');
_console.log('there');
expect(stdout()).toEqual(` ${chalk.bold('first')}
hey
${chalk.bold('second')}
there`);
});
test('groupEnd remove the indentation of the current group', () => {
_console.group();
_console.log('hey');
_console.groupEnd();
_console.log('there');
expect(stdout()).toEqual(' hey\nthere');
});
test('groupEnd can not remove the indentation below the starting point', () => {
_console.groupEnd();
_console.groupEnd();
_console.group();
_console.log('hey');
_console.groupEnd();
_console.log('there');
expect(stdout()).toEqual(' hey\nthere');
});
});
describe('time', () => {
test('should return the time between time() and timeEnd() on default timer', () => {
_console.time();
_console.timeEnd();
expect(stdout()).toMatch('default: ');
expect(stdout()).toMatch('ms');
});
test('should return the time between time() and timeEnd() on custom timer', () => {
_console.time('custom');
_console.timeEnd('custom');
expect(stdout()).toMatch('custom: ');
expect(stdout()).toMatch('ms');
});
});
describe('timeLog', () => {
test('should return the time between time() and timeEnd() on default timer', () => {
_console.time();
_console.timeLog();
expect(stdout()).toMatch('default: ');
expect(stdout()).toMatch('ms');
_console.timeEnd();
});
test('should return the time between time() and timeEnd() on custom timer', () => {
_console.time('custom');
_console.timeLog('custom');
expect(stdout()).toMatch('custom: ');
expect(stdout()).toMatch('ms');
_console.timeEnd('custom');
});
test('default timer with data', () => {
_console.time();
_console.timeLog(undefined, 'foo', 5);
expect(stdout()).toMatch('default: ');
expect(stdout()).toMatch('ms foo 5');
_console.timeEnd();
});
test('custom timer with data', () => {
_console.time('custom');
_console.timeLog('custom', 'foo', 5);
expect(stdout()).toMatch('custom: ');
expect(stdout()).toMatch('ms foo 5');
_console.timeEnd('custom');
});
});
describe('console', () => {
test('should be able to initialize console instance', () => {
expect(_console.Console).toBeDefined();
});
});
});