UNPKG

4.73 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function _assert() {
9 const data = _interopRequireDefault(require('assert'));
10
11 _assert = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _console() {
19 const data = require('console');
20
21 _console = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _util() {
29 const data = require('util');
30
31 _util = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _chalk() {
39 const data = _interopRequireDefault(require('chalk'));
40
41 _chalk = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _jestUtil() {
49 const data = require('jest-util');
50
51 _jestUtil = function () {
52 return data;
53 };
54
55 return data;
56}
57
58function _interopRequireDefault(obj) {
59 return obj && obj.__esModule ? obj : {default: obj};
60}
61
62function _defineProperty(obj, key, value) {
63 if (key in obj) {
64 Object.defineProperty(obj, key, {
65 value: value,
66 enumerable: true,
67 configurable: true,
68 writable: true
69 });
70 } else {
71 obj[key] = value;
72 }
73 return obj;
74}
75
76class CustomConsole extends _console().Console {
77 constructor(stdout, stderr, formatBuffer = (_type, message) => message) {
78 super(stdout, stderr);
79
80 _defineProperty(this, '_stdout', void 0);
81
82 _defineProperty(this, '_stderr', void 0);
83
84 _defineProperty(this, '_formatBuffer', void 0);
85
86 _defineProperty(this, '_counters', {});
87
88 _defineProperty(this, '_timers', {});
89
90 _defineProperty(this, '_groupDepth', 0);
91
92 _defineProperty(this, 'Console', _console().Console);
93
94 this._stdout = stdout;
95 this._stderr = stderr;
96 this._formatBuffer = formatBuffer;
97 }
98
99 _log(type, message) {
100 (0, _jestUtil().clearLine)(this._stdout);
101 super.log(
102 this._formatBuffer(type, ' '.repeat(this._groupDepth) + message)
103 );
104 }
105
106 _logError(type, message) {
107 (0, _jestUtil().clearLine)(this._stderr);
108 super.error(
109 this._formatBuffer(type, ' '.repeat(this._groupDepth) + message)
110 );
111 }
112
113 assert(value, message) {
114 try {
115 (0, _assert().default)(value, message);
116 } catch (error) {
117 this._logError('assert', error.toString());
118 }
119 }
120
121 count(label = 'default') {
122 if (!this._counters[label]) {
123 this._counters[label] = 0;
124 }
125
126 this._log(
127 'count',
128 (0, _util().format)(`${label}: ${++this._counters[label]}`)
129 );
130 }
131
132 countReset(label = 'default') {
133 this._counters[label] = 0;
134 }
135
136 debug(firstArg, ...args) {
137 this._log('debug', (0, _util().format)(firstArg, ...args));
138 }
139
140 dir(firstArg, options = {}) {
141 const representation = (0, _util().inspect)(firstArg, options);
142
143 this._log('dir', (0, _util().formatWithOptions)(options, representation));
144 }
145
146 dirxml(firstArg, ...args) {
147 this._log('dirxml', (0, _util().format)(firstArg, ...args));
148 }
149
150 error(firstArg, ...args) {
151 this._logError('error', (0, _util().format)(firstArg, ...args));
152 }
153
154 group(title, ...args) {
155 this._groupDepth++;
156
157 if (title || args.length > 0) {
158 this._log(
159 'group',
160 _chalk().default.bold((0, _util().format)(title, ...args))
161 );
162 }
163 }
164
165 groupCollapsed(title, ...args) {
166 this._groupDepth++;
167
168 if (title || args.length > 0) {
169 this._log(
170 'groupCollapsed',
171 _chalk().default.bold((0, _util().format)(title, ...args))
172 );
173 }
174 }
175
176 groupEnd() {
177 if (this._groupDepth > 0) {
178 this._groupDepth--;
179 }
180 }
181
182 info(firstArg, ...args) {
183 this._log('info', (0, _util().format)(firstArg, ...args));
184 }
185
186 log(firstArg, ...args) {
187 this._log('log', (0, _util().format)(firstArg, ...args));
188 }
189
190 time(label = 'default') {
191 if (this._timers[label]) {
192 return;
193 }
194
195 this._timers[label] = new Date();
196 }
197
198 timeEnd(label = 'default') {
199 const startTime = this._timers[label];
200
201 if (startTime) {
202 const endTime = new Date().getTime();
203 const time = endTime - startTime.getTime();
204
205 this._log(
206 'time',
207 (0, _util().format)(`${label}: ${(0, _jestUtil().formatTime)(time)}`)
208 );
209
210 delete this._timers[label];
211 }
212 }
213
214 timeLog(label = 'default', ...data) {
215 const startTime = this._timers[label];
216
217 if (startTime) {
218 const endTime = new Date();
219 const time = endTime.getTime() - startTime.getTime();
220
221 this._log(
222 'time',
223 (0, _util().format)(
224 `${label}: ${(0, _jestUtil().formatTime)(time)}`,
225 ...data
226 )
227 );
228 }
229 }
230
231 warn(firstArg, ...args) {
232 this._logError('warn', (0, _util().format)(firstArg, ...args));
233 }
234
235 getBuffer() {
236 return undefined;
237 }
238}
239
240exports.default = CustomConsole;