1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | function _chalk() {
|
9 | const data = _interopRequireDefault(require('chalk'));
|
10 |
|
11 | _chalk = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _jestUtil() {
|
19 | const data = require('jest-util');
|
20 |
|
21 | _jestUtil = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | var _BaseReporter = _interopRequireDefault(require('./BaseReporter'));
|
29 |
|
30 | var _getResultHeader = _interopRequireDefault(require('./getResultHeader'));
|
31 |
|
32 | var _getSnapshotSummary = _interopRequireDefault(
|
33 | require('./getSnapshotSummary')
|
34 | );
|
35 |
|
36 | var _getSummary = _interopRequireDefault(require('./getSummary'));
|
37 |
|
38 | function _interopRequireDefault(obj) {
|
39 | return obj && obj.__esModule ? obj : {default: obj};
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const TEST_SUMMARY_THRESHOLD = 20;
|
49 | const NPM_EVENTS = new Set([
|
50 | 'prepublish',
|
51 | 'publish',
|
52 | 'postpublish',
|
53 | 'preinstall',
|
54 | 'install',
|
55 | 'postinstall',
|
56 | 'preuninstall',
|
57 | 'uninstall',
|
58 | 'postuninstall',
|
59 | 'preversion',
|
60 | 'version',
|
61 | 'postversion',
|
62 | 'pretest',
|
63 | 'test',
|
64 | 'posttest',
|
65 | 'prestop',
|
66 | 'stop',
|
67 | 'poststop',
|
68 | 'prestart',
|
69 | 'start',
|
70 | 'poststart',
|
71 | 'prerestart',
|
72 | 'restart',
|
73 | 'postrestart'
|
74 | ]);
|
75 | const {npm_config_user_agent, npm_lifecycle_event, npm_lifecycle_script} =
|
76 | process.env;
|
77 |
|
78 | class SummaryReporter extends _BaseReporter.default {
|
79 | _estimatedTime;
|
80 | _globalConfig;
|
81 | static filename = __filename;
|
82 |
|
83 | constructor(globalConfig) {
|
84 | super();
|
85 | this._globalConfig = globalConfig;
|
86 | this._estimatedTime = 0;
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | _write(string) {
|
94 | for (let i = 0; i < string.length; i++) {
|
95 | process.stderr.write(string.charAt(i));
|
96 | }
|
97 | }
|
98 |
|
99 | onRunStart(aggregatedResults, options) {
|
100 | super.onRunStart(aggregatedResults, options);
|
101 | this._estimatedTime = options.estimatedTime;
|
102 | }
|
103 |
|
104 | onRunComplete(testContexts, aggregatedResults) {
|
105 | const {numTotalTestSuites, testResults, wasInterrupted} = aggregatedResults;
|
106 |
|
107 | if (numTotalTestSuites) {
|
108 | const lastResult = testResults[testResults.length - 1];
|
109 |
|
110 |
|
111 | if (
|
112 | !this._globalConfig.verbose &&
|
113 | lastResult &&
|
114 | !lastResult.numFailingTests &&
|
115 | !lastResult.testExecError
|
116 | ) {
|
117 | this.log('');
|
118 | }
|
119 |
|
120 | this._printSummary(aggregatedResults, this._globalConfig);
|
121 |
|
122 | this._printSnapshotSummary(
|
123 | aggregatedResults.snapshot,
|
124 | this._globalConfig
|
125 | );
|
126 |
|
127 | if (numTotalTestSuites) {
|
128 | let message = (0, _getSummary.default)(aggregatedResults, {
|
129 | estimatedTime: this._estimatedTime
|
130 | });
|
131 |
|
132 | if (!this._globalConfig.silent) {
|
133 | message += `\n${
|
134 | wasInterrupted
|
135 | ? _chalk().default.bold.red('Test run was interrupted.')
|
136 | : this._getTestSummary(testContexts, this._globalConfig)
|
137 | }`;
|
138 | }
|
139 |
|
140 | this.log(message);
|
141 | }
|
142 | }
|
143 | }
|
144 |
|
145 | _printSnapshotSummary(snapshots, globalConfig) {
|
146 | if (
|
147 | snapshots.added ||
|
148 | snapshots.filesRemoved ||
|
149 | snapshots.unchecked ||
|
150 | snapshots.unmatched ||
|
151 | snapshots.updated
|
152 | ) {
|
153 | let updateCommand;
|
154 | const event = npm_lifecycle_event || '';
|
155 | const prefix = NPM_EVENTS.has(event) ? '' : 'run ';
|
156 | const isYarn =
|
157 | typeof npm_config_user_agent === 'string' &&
|
158 | npm_config_user_agent.includes('yarn');
|
159 | const client = isYarn ? 'yarn' : 'npm';
|
160 | const scriptUsesJest =
|
161 | typeof npm_lifecycle_script === 'string' &&
|
162 | npm_lifecycle_script.includes('jest');
|
163 |
|
164 | if (globalConfig.watch || globalConfig.watchAll) {
|
165 | updateCommand = 'press `u`';
|
166 | } else if (event && scriptUsesJest) {
|
167 | updateCommand = `run \`${`${client} ${prefix}${event}${
|
168 | isYarn ? '' : ' --'
|
169 | }`} -u\``;
|
170 | } else {
|
171 | updateCommand = 're-run jest with `-u`';
|
172 | }
|
173 |
|
174 | const snapshotSummary = (0, _getSnapshotSummary.default)(
|
175 | snapshots,
|
176 | globalConfig,
|
177 | updateCommand
|
178 | );
|
179 | snapshotSummary.forEach(this.log);
|
180 | this.log('');
|
181 | }
|
182 | }
|
183 |
|
184 | _printSummary(aggregatedResults, globalConfig) {
|
185 |
|
186 |
|
187 | const failedTests = aggregatedResults.numFailedTests;
|
188 | const runtimeErrors = aggregatedResults.numRuntimeErrorTestSuites;
|
189 |
|
190 | if (
|
191 | failedTests + runtimeErrors > 0 &&
|
192 | aggregatedResults.numTotalTestSuites > TEST_SUMMARY_THRESHOLD
|
193 | ) {
|
194 | this.log(_chalk().default.bold('Summary of all failing tests'));
|
195 | aggregatedResults.testResults.forEach(testResult => {
|
196 | const {failureMessage} = testResult;
|
197 |
|
198 | if (failureMessage) {
|
199 | this._write(
|
200 | `${(0, _getResultHeader.default)(
|
201 | testResult,
|
202 | globalConfig
|
203 | )}\n${failureMessage}\n`
|
204 | );
|
205 | }
|
206 | });
|
207 | this.log('');
|
208 | }
|
209 | }
|
210 |
|
211 | _getTestSummary(testContexts, globalConfig) {
|
212 | const getMatchingTestsInfo = () => {
|
213 | const prefix = globalConfig.findRelatedTests
|
214 | ? ' related to files matching '
|
215 | : ' matching ';
|
216 | return (
|
217 | _chalk().default.dim(prefix) +
|
218 | (0, _jestUtil().testPathPatternToRegExp)(
|
219 | globalConfig.testPathPattern
|
220 | ).toString()
|
221 | );
|
222 | };
|
223 |
|
224 | let testInfo = '';
|
225 |
|
226 | if (globalConfig.runTestsByPath) {
|
227 | testInfo = _chalk().default.dim(' within paths');
|
228 | } else if (globalConfig.onlyChanged) {
|
229 | testInfo = _chalk().default.dim(' related to changed files');
|
230 | } else if (globalConfig.testPathPattern) {
|
231 | testInfo = getMatchingTestsInfo();
|
232 | }
|
233 |
|
234 | let nameInfo = '';
|
235 |
|
236 | if (globalConfig.runTestsByPath) {
|
237 | nameInfo = ` ${globalConfig.nonFlagArgs.map(p => `"${p}"`).join(', ')}`;
|
238 | } else if (globalConfig.testNamePattern) {
|
239 | nameInfo = `${_chalk().default.dim(' with tests matching ')}"${
|
240 | globalConfig.testNamePattern
|
241 | }"`;
|
242 | }
|
243 |
|
244 | const contextInfo =
|
245 | testContexts.size > 1
|
246 | ? _chalk().default.dim(' in ') +
|
247 | testContexts.size +
|
248 | _chalk().default.dim(' projects')
|
249 | : '';
|
250 | return (
|
251 | _chalk().default.dim('Ran all test suites') +
|
252 | testInfo +
|
253 | nameInfo +
|
254 | contextInfo +
|
255 | _chalk().default.dim('.')
|
256 | );
|
257 | }
|
258 | }
|
259 |
|
260 | exports.default = SummaryReporter;
|