UNPKG

7.13 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 _jestUtil() {
19 const data = require('jest-util');
20
21 _jestUtil = function () {
22 return data;
23 };
24
25 return data;
26}
27
28var _BaseReporter = _interopRequireDefault(require('./BaseReporter'));
29
30var _getResultHeader = _interopRequireDefault(require('./getResultHeader'));
31
32var _getSnapshotSummary = _interopRequireDefault(
33 require('./getSnapshotSummary')
34);
35
36var _utils = require('./utils');
37
38function _interopRequireDefault(obj) {
39 return obj && obj.__esModule ? obj : {default: obj};
40}
41
42function _defineProperty(obj, key, value) {
43 if (key in obj) {
44 Object.defineProperty(obj, key, {
45 value: value,
46 enumerable: true,
47 configurable: true,
48 writable: true
49 });
50 } else {
51 obj[key] = value;
52 }
53 return obj;
54}
55
56const TEST_SUMMARY_THRESHOLD = 20;
57const NPM_EVENTS = new Set([
58 'prepublish',
59 'publish',
60 'postpublish',
61 'preinstall',
62 'install',
63 'postinstall',
64 'preuninstall',
65 'uninstall',
66 'postuninstall',
67 'preversion',
68 'version',
69 'postversion',
70 'pretest',
71 'test',
72 'posttest',
73 'prestop',
74 'stop',
75 'poststop',
76 'prestart',
77 'start',
78 'poststart',
79 'prerestart',
80 'restart',
81 'postrestart'
82]);
83const {npm_config_user_agent, npm_lifecycle_event, npm_lifecycle_script} =
84 process.env;
85
86class SummaryReporter extends _BaseReporter.default {
87 constructor(globalConfig) {
88 super();
89
90 _defineProperty(this, '_estimatedTime', void 0);
91
92 _defineProperty(this, '_globalConfig', void 0);
93
94 this._globalConfig = globalConfig;
95 this._estimatedTime = 0;
96 } // If we write more than one character at a time it is possible that
97 // Node.js exits in the middle of printing the result. This was first observed
98 // in Node.js 0.10 and still persists in Node.js 6.7+.
99 // Let's print the test failure summary character by character which is safer
100 // when hundreds of tests are failing.
101
102 _write(string) {
103 for (let i = 0; i < string.length; i++) {
104 process.stderr.write(string.charAt(i));
105 }
106 }
107
108 onRunStart(aggregatedResults, options) {
109 super.onRunStart(aggregatedResults, options);
110 this._estimatedTime = options.estimatedTime;
111 }
112
113 onRunComplete(contexts, aggregatedResults) {
114 const {numTotalTestSuites, testResults, wasInterrupted} = aggregatedResults;
115
116 if (numTotalTestSuites) {
117 const lastResult = testResults[testResults.length - 1]; // Print a newline if the last test did not fail to line up newlines
118 // similar to when an error would have been thrown in the test.
119
120 if (
121 !this._globalConfig.verbose &&
122 lastResult &&
123 !lastResult.numFailingTests &&
124 !lastResult.testExecError
125 ) {
126 this.log('');
127 }
128
129 this._printSummary(aggregatedResults, this._globalConfig);
130
131 this._printSnapshotSummary(
132 aggregatedResults.snapshot,
133 this._globalConfig
134 );
135
136 if (numTotalTestSuites) {
137 let message = (0, _utils.getSummary)(aggregatedResults, {
138 estimatedTime: this._estimatedTime
139 });
140
141 if (!this._globalConfig.silent) {
142 message +=
143 '\n' +
144 (wasInterrupted
145 ? _chalk().default.bold.red('Test run was interrupted.')
146 : this._getTestSummary(contexts, this._globalConfig));
147 }
148
149 this.log(message);
150 }
151 }
152 }
153
154 _printSnapshotSummary(snapshots, globalConfig) {
155 if (
156 snapshots.added ||
157 snapshots.filesRemoved ||
158 snapshots.unchecked ||
159 snapshots.unmatched ||
160 snapshots.updated
161 ) {
162 let updateCommand;
163 const event = npm_lifecycle_event || '';
164 const prefix = NPM_EVENTS.has(event) ? '' : 'run ';
165 const isYarn =
166 typeof npm_config_user_agent === 'string' &&
167 npm_config_user_agent.includes('yarn');
168 const client = isYarn ? 'yarn' : 'npm';
169 const scriptUsesJest =
170 typeof npm_lifecycle_script === 'string' &&
171 npm_lifecycle_script.includes('jest');
172
173 if (globalConfig.watch || globalConfig.watchAll) {
174 updateCommand = 'press `u`';
175 } else if (event && scriptUsesJest) {
176 updateCommand = `run \`${
177 client + ' ' + prefix + event + (isYarn ? '' : ' --')
178 } -u\``;
179 } else {
180 updateCommand = 're-run jest with `-u`';
181 }
182
183 const snapshotSummary = (0, _getSnapshotSummary.default)(
184 snapshots,
185 globalConfig,
186 updateCommand
187 );
188 snapshotSummary.forEach(this.log);
189 this.log(''); // print empty line
190 }
191 }
192
193 _printSummary(aggregatedResults, globalConfig) {
194 // If there were any failing tests and there was a large number of tests
195 // executed, re-print the failing results at the end of execution output.
196 const failedTests = aggregatedResults.numFailedTests;
197 const runtimeErrors = aggregatedResults.numRuntimeErrorTestSuites;
198
199 if (
200 failedTests + runtimeErrors > 0 &&
201 aggregatedResults.numTotalTestSuites > TEST_SUMMARY_THRESHOLD
202 ) {
203 this.log(_chalk().default.bold('Summary of all failing tests'));
204 aggregatedResults.testResults.forEach(testResult => {
205 const {failureMessage} = testResult;
206
207 if (failureMessage) {
208 this._write(
209 (0, _getResultHeader.default)(testResult, globalConfig) +
210 '\n' +
211 failureMessage +
212 '\n'
213 );
214 }
215 });
216 this.log(''); // print empty line
217 }
218 }
219
220 _getTestSummary(contexts, globalConfig) {
221 const getMatchingTestsInfo = () => {
222 const prefix = globalConfig.findRelatedTests
223 ? ' related to files matching '
224 : ' matching ';
225 return (
226 _chalk().default.dim(prefix) +
227 (0, _jestUtil().testPathPatternToRegExp)(
228 globalConfig.testPathPattern
229 ).toString()
230 );
231 };
232
233 let testInfo = '';
234
235 if (globalConfig.runTestsByPath) {
236 testInfo = _chalk().default.dim(' within paths');
237 } else if (globalConfig.onlyChanged) {
238 testInfo = _chalk().default.dim(' related to changed files');
239 } else if (globalConfig.testPathPattern) {
240 testInfo = getMatchingTestsInfo();
241 }
242
243 let nameInfo = '';
244
245 if (globalConfig.runTestsByPath) {
246 nameInfo = ' ' + globalConfig.nonFlagArgs.map(p => `"${p}"`).join(', ');
247 } else if (globalConfig.testNamePattern) {
248 nameInfo =
249 _chalk().default.dim(' with tests matching ') +
250 `"${globalConfig.testNamePattern}"`;
251 }
252
253 const contextInfo =
254 contexts.size > 1
255 ? _chalk().default.dim(' in ') +
256 contexts.size +
257 _chalk().default.dim(' projects')
258 : '';
259 return (
260 _chalk().default.dim('Ran all test suites') +
261 testInfo +
262 nameInfo +
263 contextInfo +
264 _chalk().default.dim('.')
265 );
266 }
267}
268
269exports.default = SummaryReporter;
270
271_defineProperty(SummaryReporter, 'filename', __filename);