/
CustomConsole.test.ts
186 lines (147 loc) · 4.57 KB
/
CustomConsole.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
/**
* 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 {Writable} from 'stream';
import chalk from 'chalk';
import CustomConsole from '../CustomConsole';
describe('CustomConsole', () => {
let _console;
let _stdout;
let _stderr;
beforeEach(() => {
_stdout = '';
_stderr = '';
const stdout = new Writable({
write(chunk, encoding, callback) {
_stdout += chunk.toString();
callback();
},
});
const stderr = new Writable({
write(chunk, encoding, callback) {
_stderr += chunk.toString();
callback();
},
});
_console = new CustomConsole(stdout, stderr);
});
describe('log', () => {
test('should print to stdout', () => {
_console.log('Hello world!');
expect(_stdout).toBe('Hello world!\n');
});
});
describe('error', () => {
test('should print to stderr', () => {
_console.error('Found some error!');
expect(_stderr).toBe('Found some error!\n');
});
});
describe('warn', () => {
test('should print to stderr', () => {
_console.warn('Found some warning!');
expect(_stderr).toBe('Found some warning!\n');
});
});
describe('assert', () => {
test('do not log when the assertion is truthy', () => {
_console.assert(true);
expect(_stderr).toMatch('');
});
test('do not log when the assertion is truthy and there is a message', () => {
_console.assert(true, 'ok');
expect(_stderr).toMatch('');
});
test('log the assertion error when the assertion is falsy', () => {
_console.assert(false);
expect(_stderr).toMatch('AssertionError');
expect(_stderr).toMatch('The expression evaluated to a falsy value:');
});
test('log the assertion error when the assertion is falsy with another message argument', () => {
_console.assert(false, 'ok');
expect(_stderr).toMatch('AssertionError');
expect(_stderr).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\n');
});
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\n');
});
test('countReset restarts default counter', () => {
_console.count();
_console.count();
_console.countReset();
_console.count();
expect(_stdout).toEqual('default: 1\ndefault: 2\ndefault: 1\n');
});
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\n');
});
});
describe('group', () => {
test('group without label', () => {
_console.group();
_console.log('hey');
_console.group();
_console.log('there');
expect(_stdout).toEqual(' hey\n there\n');
});
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\n');
});
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\n');
});
});
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');
});
});
});