UNPKG

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