-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
utils.ts
274 lines (247 loc) · 8.88 KB
/
utils.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* 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 * as path from 'path';
import {Config} from '@jest/types';
import {AggregatedResult} from '@jest/test-result';
import chalk = require('chalk');
import slash = require('slash');
import {pluralize} from 'jest-util';
import {SummaryOptions} from './types';
const PROGRESS_BAR_WIDTH = 40;
export const printDisplayName = (config: Config.ProjectConfig) => {
const {displayName} = config;
const white = chalk.reset.inverse.white;
if (!displayName) {
return '';
}
if (typeof displayName === 'string') {
return chalk.supportsColor ? white(` ${displayName} `) : displayName;
}
const {name, color} = displayName;
const chosenColor = chalk.reset.inverse[color]
? chalk.reset.inverse[color]
: white;
return chalk.supportsColor ? chosenColor(` ${name} `) : name;
};
export const trimAndFormatPath = (
pad: number,
config: Config.ProjectConfig | Config.GlobalConfig,
testPath: Config.Path,
columns: number,
): string => {
const maxLength = columns - pad;
const relative = relativePath(config, testPath);
const {basename} = relative;
let {dirname} = relative;
// length is ok
if ((dirname + path.sep + basename).length <= maxLength) {
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
}
// we can fit trimmed dirname and full basename
const basenameLength = basename.length;
if (basenameLength + 4 < maxLength) {
const dirnameLength = maxLength - 4 - basenameLength;
dirname =
'...' + dirname.slice(dirname.length - dirnameLength, dirname.length);
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
}
if (basenameLength + 4 === maxLength) {
return slash(chalk.dim('...' + path.sep) + chalk.bold(basename));
}
// can't fit dirname, but can fit trimmed basename
return slash(
chalk.bold(
'...' + basename.slice(basename.length - maxLength - 4, basename.length),
),
);
};
export const formatTestPath = (
config: Config.GlobalConfig | Config.ProjectConfig,
testPath: Config.Path,
) => {
const {dirname, basename} = relativePath(config, testPath);
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
};
export const relativePath = (
config: Config.GlobalConfig | Config.ProjectConfig,
testPath: Config.Path,
) => {
// this function can be called with ProjectConfigs or GlobalConfigs. GlobalConfigs
// do not have config.cwd, only config.rootDir. Try using config.cwd, fallback
// to config.rootDir. (Also, some unit just use config.rootDir, which is ok)
testPath = path.relative(
(config as Config.ProjectConfig).cwd || config.rootDir,
testPath,
);
const dirname = path.dirname(testPath);
const basename = path.basename(testPath);
return {basename, dirname};
};
export const getSummary = (
aggregatedResults: AggregatedResult,
options?: SummaryOptions,
) => {
let runTime = (Date.now() - aggregatedResults.startTime) / 1000;
if (options && options.roundTime) {
runTime = Math.floor(runTime);
}
const estimatedTime = (options && options.estimatedTime) || 0;
const snapshotResults = aggregatedResults.snapshot;
const snapshotsAdded = snapshotResults.added;
const snapshotsFailed = snapshotResults.unmatched;
const snapshotsOutdated = snapshotResults.unchecked;
const snapshotsFilesRemoved = snapshotResults.filesRemoved;
const snapshotsDidUpdate = snapshotResults.didUpdate;
const snapshotsPassed = snapshotResults.matched;
const snapshotsTotal = snapshotResults.total;
const snapshotsUpdated = snapshotResults.updated;
const suitesFailed = aggregatedResults.numFailedTestSuites;
const suitesPassed = aggregatedResults.numPassedTestSuites;
const suitesPending = aggregatedResults.numPendingTestSuites;
const suitesRun = suitesFailed + suitesPassed;
const suitesTotal = aggregatedResults.numTotalTestSuites;
const testsFailed = aggregatedResults.numFailedTests;
const testsPassed = aggregatedResults.numPassedTests;
const testsPending = aggregatedResults.numPendingTests;
const testsTodo = aggregatedResults.numTodoTests;
const testsTotal = aggregatedResults.numTotalTests;
const width = (options && options.width) || 0;
const suites =
chalk.bold('Test Suites: ') +
(suitesFailed ? chalk.bold.red(`${suitesFailed} failed`) + ', ' : '') +
(suitesPending
? chalk.bold.yellow(`${suitesPending} skipped`) + ', '
: '') +
(suitesPassed ? chalk.bold.green(`${suitesPassed} passed`) + ', ' : '') +
(suitesRun !== suitesTotal
? suitesRun + ' of ' + suitesTotal
: suitesTotal) +
` total`;
const tests =
chalk.bold('Tests: ') +
(testsFailed ? chalk.bold.red(`${testsFailed} failed`) + ', ' : '') +
(testsPending ? chalk.bold.yellow(`${testsPending} skipped`) + ', ' : '') +
(testsTodo ? chalk.bold.magenta(`${testsTodo} todo`) + ', ' : '') +
(testsPassed ? chalk.bold.green(`${testsPassed} passed`) + ', ' : '') +
`${testsTotal} total`;
const snapshots =
chalk.bold('Snapshots: ') +
(snapshotsFailed
? chalk.bold.red(`${snapshotsFailed} failed`) + ', '
: '') +
(snapshotsOutdated && !snapshotsDidUpdate
? chalk.bold.yellow(`${snapshotsOutdated} obsolete`) + ', '
: '') +
(snapshotsOutdated && snapshotsDidUpdate
? chalk.bold.green(`${snapshotsOutdated} removed`) + ', '
: '') +
(snapshotsFilesRemoved && !snapshotsDidUpdate
? chalk.bold.yellow(
pluralize('file', snapshotsFilesRemoved) + ' obsolete',
) + ', '
: '') +
(snapshotsFilesRemoved && snapshotsDidUpdate
? chalk.bold.green(
pluralize('file', snapshotsFilesRemoved) + ' removed',
) + ', '
: '') +
(snapshotsUpdated
? chalk.bold.green(`${snapshotsUpdated} updated`) + ', '
: '') +
(snapshotsAdded
? chalk.bold.green(`${snapshotsAdded} written`) + ', '
: '') +
(snapshotsPassed
? chalk.bold.green(`${snapshotsPassed} passed`) + ', '
: '') +
`${snapshotsTotal} total`;
const time = renderTime(runTime, estimatedTime, width);
return [suites, tests, snapshots, time].join('\n');
};
const renderTime = (runTime: number, estimatedTime: number, width: number) => {
// If we are more than one second over the estimated time, highlight it.
const renderedTime =
estimatedTime && runTime >= estimatedTime + 1
? chalk.bold.yellow(runTime + 's')
: runTime + 's';
let time = chalk.bold(`Time:`) + ` ${renderedTime}`;
if (runTime < estimatedTime) {
time += `, estimated ${estimatedTime}s`;
}
// Only show a progress bar if the test run is actually going to take
// some time.
if (estimatedTime > 2 && runTime < estimatedTime && width) {
const availableWidth = Math.min(PROGRESS_BAR_WIDTH, width);
const length = Math.min(
Math.floor((runTime / estimatedTime) * availableWidth),
availableWidth,
);
if (availableWidth >= 2) {
time +=
'\n' +
chalk.green('█').repeat(length) +
chalk.white('█').repeat(availableWidth - length);
}
}
return time;
};
// word-wrap a string that contains ANSI escape sequences.
// ANSI escape sequences do not add to the string length.
export const wrapAnsiString = (string: string, terminalWidth: number) => {
if (terminalWidth === 0) {
// if the terminal width is zero, don't bother word-wrapping
return string;
}
const ANSI_REGEXP = /[\u001b\u009b]\[\d{1,2}m/g;
const tokens = [];
let lastIndex = 0;
let match;
while ((match = ANSI_REGEXP.exec(string))) {
const ansi = match[0];
const index = match['index'];
if (index != lastIndex) {
tokens.push(['string', string.slice(lastIndex, index)]);
}
tokens.push(['ansi', ansi]);
lastIndex = index + ansi.length;
}
if (lastIndex != string.length - 1) {
tokens.push(['string', string.slice(lastIndex, string.length)]);
}
let lastLineLength = 0;
return tokens
.reduce(
(lines, [kind, token]) => {
if (kind === 'string') {
if (lastLineLength + token.length > terminalWidth) {
while (token.length) {
const chunk = token.slice(0, terminalWidth - lastLineLength);
const remaining = token.slice(
terminalWidth - lastLineLength,
token.length,
);
lines[lines.length - 1] += chunk;
lastLineLength += chunk.length;
token = remaining;
if (token.length) {
lines.push('');
lastLineLength = 0;
}
}
} else {
lines[lines.length - 1] += token;
lastLineLength += token.length;
}
} else {
lines[lines.length - 1] += token;
}
return lines;
},
[''],
)
.join('\n');
};