-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathsummary_reporter.ts
235 lines (211 loc) · 6.59 KB
/
summary_reporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Config} from '@jest/types';
import {AggregatedResult, SnapshotSummary} from '@jest/test-result';
import chalk from 'chalk';
import {testPathPatternToRegExp} from 'jest-util';
import {Context, ReporterOnStartOptions} from './types';
import BaseReporter from './base_reporter';
import {getSummary} from './utils';
import getResultHeader from './get_result_header';
import getSnapshotSummary from './get_snapshot_summary';
const TEST_SUMMARY_THRESHOLD = 20;
const NPM_EVENTS = new Set([
'prepublish',
'publish',
'postpublish',
'preinstall',
'install',
'postinstall',
'preuninstall',
'uninstall',
'postuninstall',
'preversion',
'version',
'postversion',
'pretest',
'test',
'posttest',
'prestop',
'stop',
'poststop',
'prestart',
'start',
'poststart',
'prerestart',
'restart',
'postrestart',
]);
export default class SummaryReporter extends BaseReporter {
private _estimatedTime: number;
private _globalConfig: Config.GlobalConfig;
constructor(globalConfig: Config.GlobalConfig) {
super();
this._globalConfig = globalConfig;
this._estimatedTime = 0;
}
// If we write more than one character at a time it is possible that
// Node.js exits in the middle of printing the result. This was first observed
// in Node.js 0.10 and still persists in Node.js 6.7+.
// Let's print the test failure summary character by character which is safer
// when hundreds of tests are failing.
private _write(string: string) {
for (let i = 0; i < string.length; i++) {
process.stderr.write(string.charAt(i));
}
}
onRunStart(
aggregatedResults: AggregatedResult,
options: ReporterOnStartOptions,
) {
super.onRunStart(aggregatedResults, options);
this._estimatedTime = options.estimatedTime;
}
onRunComplete(contexts: Set<Context>, aggregatedResults: AggregatedResult) {
const {numTotalTestSuites, testResults, wasInterrupted} = aggregatedResults;
if (numTotalTestSuites) {
const lastResult = testResults[testResults.length - 1];
// Print a newline if the last test did not fail to line up newlines
// similar to when an error would have been thrown in the test.
if (
!this._globalConfig.verbose &&
lastResult &&
!lastResult.numFailingTests &&
!lastResult.testExecError
) {
this.log('');
}
this._printSummary(aggregatedResults, this._globalConfig);
this._printSnapshotSummary(
aggregatedResults.snapshot,
this._globalConfig,
);
if (numTotalTestSuites) {
let message = getSummary(aggregatedResults, {
estimatedTime: this._estimatedTime,
});
if (!this._globalConfig.silent) {
message +=
'\n' +
(wasInterrupted
? chalk.bold.red('Test run was interrupted.')
: this._getTestSummary(contexts, this._globalConfig));
}
this.log(message);
}
}
}
private _printSnapshotSummary(
snapshots: SnapshotSummary,
globalConfig: Config.GlobalConfig,
) {
if (
snapshots.added ||
snapshots.filesRemoved ||
snapshots.unchecked ||
snapshots.unmatched ||
snapshots.updated
) {
let updateCommand;
const event = process.env.npm_lifecycle_event || '';
const prefix = NPM_EVENTS.has(event) ? '' : 'run ';
const isYarn =
typeof process.env.npm_config_user_agent === 'string' &&
process.env.npm_config_user_agent.match('yarn') !== null;
const client = isYarn ? 'yarn' : 'npm';
const scriptUsesJest =
typeof process.env.npm_lifecycle_script === 'string' &&
process.env.npm_lifecycle_script.indexOf('jest') !== -1;
if (globalConfig.watch || globalConfig.watchAll) {
updateCommand = 'press `u`';
} else if (event && scriptUsesJest) {
updateCommand = `run \`${client +
' ' +
prefix +
event +
(isYarn ? '' : ' --')} -u\``;
} else {
updateCommand = 're-run jest with `-u`';
}
const snapshotSummary = getSnapshotSummary(
snapshots,
globalConfig,
updateCommand,
);
snapshotSummary.forEach(this.log);
this.log(''); // print empty line
}
}
private _printSummary(
aggregatedResults: AggregatedResult,
globalConfig: Config.GlobalConfig,
) {
// If there were any failing tests and there was a large number of tests
// executed, re-print the failing results at the end of execution output.
const failedTests = aggregatedResults.numFailedTests;
const runtimeErrors = aggregatedResults.numRuntimeErrorTestSuites;
if (
failedTests + runtimeErrors > 0 &&
aggregatedResults.numTotalTestSuites > TEST_SUMMARY_THRESHOLD
) {
this.log(chalk.bold('Summary of all failing tests'));
aggregatedResults.testResults.forEach(testResult => {
const {failureMessage} = testResult;
if (failureMessage) {
this._write(
getResultHeader(testResult, globalConfig) +
'\n' +
failureMessage +
'\n',
);
}
});
this.log(''); // print empty line
}
}
private _getTestSummary(
contexts: Set<Context>,
globalConfig: Config.GlobalConfig,
) {
const getMatchingTestsInfo = () => {
const prefix = globalConfig.findRelatedTests
? ' related to files matching '
: ' matching ';
return (
chalk.dim(prefix) +
testPathPatternToRegExp(globalConfig.testPathPattern).toString()
);
};
let testInfo = '';
if (globalConfig.runTestsByPath) {
testInfo = chalk.dim(' within paths');
} else if (globalConfig.onlyChanged) {
testInfo = chalk.dim(' related to changed files');
} else if (globalConfig.testPathPattern) {
testInfo = getMatchingTestsInfo();
}
let nameInfo = '';
if (globalConfig.runTestsByPath) {
nameInfo = ' ' + globalConfig.nonFlagArgs.map(p => `"${p}"`).join(', ');
} else if (globalConfig.testNamePattern) {
nameInfo =
chalk.dim(' with tests matching ') +
`"${globalConfig.testNamePattern}"`;
}
const contextInfo =
contexts.size > 1
? chalk.dim(' in ') + contexts.size + chalk.dim(' projects')
: '';
return (
chalk.dim('Ran all test suites') +
testInfo +
nameInfo +
contextInfo +
chalk.dim('.')
);
}
}