UNPKG

5.8 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 _jestUtil() {
29 const data = require('jest-util');
30
31 _jestUtil = function () {
32 return data;
33 };
34
35 return data;
36}
37
38var _BaseReporter = _interopRequireDefault(require('./BaseReporter'));
39
40var _Status = _interopRequireDefault(require('./Status'));
41
42var _getResultHeader = _interopRequireDefault(require('./getResultHeader'));
43
44var _getSnapshotStatus = _interopRequireDefault(require('./getSnapshotStatus'));
45
46function _interopRequireDefault(obj) {
47 return obj && obj.__esModule ? obj : {default: obj};
48}
49
50function _defineProperty(obj, key, value) {
51 if (key in obj) {
52 Object.defineProperty(obj, key, {
53 value: value,
54 enumerable: true,
55 configurable: true,
56 writable: true
57 });
58 } else {
59 obj[key] = value;
60 }
61 return obj;
62}
63
64const TITLE_BULLET = _chalk().default.bold('\u25cf ');
65
66class DefaultReporter extends _BaseReporter.default {
67 // ANSI clear sequence for the last printed status
68 constructor(globalConfig) {
69 super();
70
71 _defineProperty(this, '_clear', void 0);
72
73 _defineProperty(this, '_err', void 0);
74
75 _defineProperty(this, '_globalConfig', void 0);
76
77 _defineProperty(this, '_out', void 0);
78
79 _defineProperty(this, '_status', void 0);
80
81 _defineProperty(this, '_bufferedOutput', void 0);
82
83 this._globalConfig = globalConfig;
84 this._clear = '';
85 this._out = process.stdout.write.bind(process.stdout);
86 this._err = process.stderr.write.bind(process.stderr);
87 this._status = new _Status.default();
88 this._bufferedOutput = new Set();
89
90 this._wrapStdio(process.stdout);
91
92 this._wrapStdio(process.stderr);
93
94 this._status.onChange(() => {
95 this._clearStatus();
96
97 this._printStatus();
98 });
99 }
100
101 _wrapStdio(stream) {
102 const originalWrite = stream.write;
103 let buffer = [];
104 let timeout = null;
105
106 const flushBufferedOutput = () => {
107 const string = buffer.join('');
108 buffer = []; // This is to avoid conflicts between random output and status text
109
110 this._clearStatus();
111
112 if (string) {
113 originalWrite.call(stream, string);
114 }
115
116 this._printStatus();
117
118 this._bufferedOutput.delete(flushBufferedOutput);
119 };
120
121 this._bufferedOutput.add(flushBufferedOutput);
122
123 const debouncedFlush = () => {
124 // If the process blows up no errors would be printed.
125 // There should be a smart way to buffer stderr, but for now
126 // we just won't buffer it.
127 if (stream === process.stderr) {
128 flushBufferedOutput();
129 } else {
130 if (!timeout) {
131 timeout = setTimeout(() => {
132 flushBufferedOutput();
133 timeout = null;
134 }, 100);
135 }
136 }
137 };
138
139 stream.write = chunk => {
140 buffer.push(chunk);
141 debouncedFlush();
142 return true;
143 };
144 } // Don't wait for the debounced call and flush all output immediately.
145
146 forceFlushBufferedOutput() {
147 for (const flushBufferedOutput of this._bufferedOutput) {
148 flushBufferedOutput();
149 }
150 }
151
152 _clearStatus() {
153 if (_jestUtil().isInteractive) {
154 if (this._globalConfig.useStderr) {
155 this._err(this._clear);
156 } else {
157 this._out(this._clear);
158 }
159 }
160 }
161
162 _printStatus() {
163 const {content, clear} = this._status.get();
164
165 this._clear = clear;
166
167 if (_jestUtil().isInteractive) {
168 if (this._globalConfig.useStderr) {
169 this._err(content);
170 } else {
171 this._out(content);
172 }
173 }
174 }
175
176 onRunStart(aggregatedResults, options) {
177 this._status.runStarted(aggregatedResults, options);
178 }
179
180 onTestStart(test) {
181 this._status.testStarted(test.path, test.context.config);
182 }
183
184 onTestCaseResult(test, testCaseResult) {
185 this._status.addTestCaseResult(test, testCaseResult);
186 }
187
188 onRunComplete() {
189 this.forceFlushBufferedOutput();
190
191 this._status.runFinished();
192
193 process.stdout.write = this._out;
194 process.stderr.write = this._err;
195 (0, _jestUtil().clearLine)(process.stderr);
196 }
197
198 onTestResult(test, testResult, aggregatedResults) {
199 this.testFinished(test.context.config, testResult, aggregatedResults);
200
201 if (!testResult.skipped) {
202 this.printTestFileHeader(
203 testResult.testFilePath,
204 test.context.config,
205 testResult
206 );
207 this.printTestFileFailureMessage(
208 testResult.testFilePath,
209 test.context.config,
210 testResult
211 );
212 }
213
214 this.forceFlushBufferedOutput();
215 }
216
217 testFinished(config, testResult, aggregatedResults) {
218 this._status.testFinished(config, testResult, aggregatedResults);
219 }
220
221 printTestFileHeader(_testPath, config, result) {
222 this.log((0, _getResultHeader.default)(result, this._globalConfig, config));
223
224 if (result.console) {
225 this.log(
226 ' ' +
227 TITLE_BULLET +
228 'Console\n\n' +
229 (0, _console().getConsoleOutput)(
230 result.console,
231 config,
232 this._globalConfig
233 )
234 );
235 }
236 }
237
238 printTestFileFailureMessage(_testPath, _config, result) {
239 if (result.failureMessage) {
240 this.log(result.failureMessage);
241 }
242
243 const didUpdate = this._globalConfig.updateSnapshot === 'all';
244 const snapshotStatuses = (0, _getSnapshotStatus.default)(
245 result.snapshot,
246 didUpdate
247 );
248 snapshotStatuses.forEach(this.log);
249 }
250}
251
252exports.default = DefaultReporter;
253
254_defineProperty(DefaultReporter, 'filename', __filename);