UNPKG

6.77 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function _chalk() {
9 const data = _interopRequireDefault(require('chalk'));
10
11 _chalk = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _console() {
19 const data = require('@jest/console');
20
21 _console = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _jestMessageUtil() {
29 const data = require('jest-message-util');
30
31 _jestMessageUtil = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _jestUtil() {
39 const data = require('jest-util');
40
41 _jestUtil = function () {
42 return data;
43 };
44
45 return data;
46}
47
48var _BaseReporter = _interopRequireDefault(require('./BaseReporter'));
49
50var _Status = _interopRequireDefault(require('./Status'));
51
52var _getResultHeader = _interopRequireDefault(require('./getResultHeader'));
53
54var _getSnapshotStatus = _interopRequireDefault(require('./getSnapshotStatus'));
55
56function _interopRequireDefault(obj) {
57 return obj && obj.__esModule ? obj : {default: obj};
58}
59
60/**
61 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
62 *
63 * This source code is licensed under the MIT license found in the
64 * LICENSE file in the root directory of this source tree.
65 */
66const TITLE_BULLET = _chalk().default.bold('\u25cf ');
67
68class DefaultReporter extends _BaseReporter.default {
69 _clear; // ANSI clear sequence for the last printed status
70
71 _err;
72 _globalConfig;
73 _out;
74 _status;
75 _bufferedOutput;
76 static filename = __filename;
77
78 constructor(globalConfig) {
79 super();
80 this._globalConfig = globalConfig;
81 this._clear = '';
82 this._out = process.stdout.write.bind(process.stdout);
83 this._err = process.stderr.write.bind(process.stderr);
84 this._status = new _Status.default();
85 this._bufferedOutput = new Set();
86
87 this.__wrapStdio(process.stdout);
88
89 this.__wrapStdio(process.stderr);
90
91 this._status.onChange(() => {
92 this.__clearStatus();
93
94 this.__printStatus();
95 });
96 }
97
98 __wrapStdio(stream) {
99 const write = stream.write.bind(stream);
100 let buffer = [];
101 let timeout = null;
102
103 const flushBufferedOutput = () => {
104 const string = buffer.join('');
105 buffer = []; // This is to avoid conflicts between random output and status text
106
107 this.__clearStatus();
108
109 if (string) {
110 write(string);
111 }
112
113 this.__printStatus();
114
115 this._bufferedOutput.delete(flushBufferedOutput);
116 };
117
118 this._bufferedOutput.add(flushBufferedOutput);
119
120 const debouncedFlush = () => {
121 // If the process blows up no errors would be printed.
122 // There should be a smart way to buffer stderr, but for now
123 // we just won't buffer it.
124 if (stream === process.stderr) {
125 flushBufferedOutput();
126 } else {
127 if (!timeout) {
128 timeout = setTimeout(() => {
129 flushBufferedOutput();
130 timeout = null;
131 }, 100);
132 }
133 }
134 };
135
136 stream.write = chunk => {
137 buffer.push(chunk);
138 debouncedFlush();
139 return true;
140 };
141 } // Don't wait for the debounced call and flush all output immediately.
142
143 forceFlushBufferedOutput() {
144 for (const flushBufferedOutput of this._bufferedOutput) {
145 flushBufferedOutput();
146 }
147 }
148
149 __clearStatus() {
150 if (_jestUtil().isInteractive) {
151 if (this._globalConfig.useStderr) {
152 this._err(this._clear);
153 } else {
154 this._out(this._clear);
155 }
156 }
157 }
158
159 __printStatus() {
160 const {content, clear} = this._status.get();
161
162 this._clear = clear;
163
164 if (_jestUtil().isInteractive) {
165 if (this._globalConfig.useStderr) {
166 this._err(content);
167 } else {
168 this._out(content);
169 }
170 }
171 }
172
173 onRunStart(aggregatedResults, options) {
174 this._status.runStarted(aggregatedResults, options);
175 }
176
177 onTestStart(test) {
178 this._status.testStarted(test.path, test.context.config);
179 }
180
181 onTestCaseResult(test, testCaseResult) {
182 this._status.addTestCaseResult(test, testCaseResult);
183 }
184
185 onRunComplete() {
186 this.forceFlushBufferedOutput();
187
188 this._status.runFinished();
189
190 process.stdout.write = this._out;
191 process.stderr.write = this._err;
192 (0, _jestUtil().clearLine)(process.stderr);
193 }
194
195 onTestResult(test, testResult, aggregatedResults) {
196 this.testFinished(test.context.config, testResult, aggregatedResults);
197
198 if (!testResult.skipped) {
199 this.printTestFileHeader(
200 testResult.testFilePath,
201 test.context.config,
202 testResult
203 );
204 this.printTestFileFailureMessage(
205 testResult.testFilePath,
206 test.context.config,
207 testResult
208 );
209 }
210
211 this.forceFlushBufferedOutput();
212 }
213
214 testFinished(config, testResult, aggregatedResults) {
215 this._status.testFinished(config, testResult, aggregatedResults);
216 }
217
218 printTestFileHeader(testPath, config, result) {
219 // log retry errors if any exist
220 result.testResults.forEach(testResult => {
221 const testRetryReasons = testResult.retryReasons;
222
223 if (testRetryReasons && testRetryReasons.length > 0) {
224 this.log(
225 `${_chalk().default.reset.inverse.bold.yellow(
226 ' LOGGING RETRY ERRORS '
227 )} ${_chalk().default.bold(testResult.fullName)}`
228 );
229 testRetryReasons.forEach((retryReasons, index) => {
230 let {message, stack} = (0,
231 _jestMessageUtil().separateMessageFromStack)(retryReasons);
232 stack = this._globalConfig.noStackTrace
233 ? ''
234 : _chalk().default.dim(
235 (0, _jestMessageUtil().formatStackTrace)(
236 stack,
237 config,
238 this._globalConfig,
239 testPath
240 )
241 );
242 message = (0, _jestMessageUtil().indentAllLines)(message);
243 this.log(
244 `${_chalk().default.reset.inverse.bold.blueBright(
245 ` RETRY ${index + 1} `
246 )}\n`
247 );
248 this.log(`${message}\n${stack}\n`);
249 });
250 }
251 });
252 this.log((0, _getResultHeader.default)(result, this._globalConfig, config));
253
254 if (result.console) {
255 this.log(
256 ` ${TITLE_BULLET}Console\n\n${(0, _console().getConsoleOutput)(
257 result.console,
258 config,
259 this._globalConfig
260 )}`
261 );
262 }
263 }
264
265 printTestFileFailureMessage(_testPath, _config, result) {
266 if (result.failureMessage) {
267 this.log(result.failureMessage);
268 }
269
270 const didUpdate = this._globalConfig.updateSnapshot === 'all';
271 const snapshotStatuses = (0, _getSnapshotStatus.default)(
272 result.snapshot,
273 didUpdate
274 );
275 snapshotStatuses.forEach(this.log);
276 }
277}
278
279exports.default = DefaultReporter;