-
Notifications
You must be signed in to change notification settings - Fork 6
/
sessionReporter.ts
212 lines (187 loc) · 6.55 KB
/
sessionReporter.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
import type {
FullConfig,
FullResult,
Reporter,
Suite,
TestCase,
TestError,
TestResult,
} from '@playwright/test/reporter';
import chalk from 'chalk';
import { Dictionary, groupBy, mean, sortBy } from 'lodash';
type TestAndResult = { test: TestCase; result: TestResult };
function sortByTitle(toSort: Dictionary<Array<TestAndResult>>) {
return sortBy(Object.values(toSort), (a) => a[0].test.title);
}
function getChalkColorForStatus(result: Pick<TestResult, 'status'>) {
return result.status === 'passed'
? chalk.green
: result.status === 'interrupted'
? chalk.yellow
: chalk.red;
}
function testResultToDurationStr(tests: Array<Pick<TestAndResult, 'result'>>) {
const inSeconds = tests
.map((m) => m.result)
.map((r) => Math.floor(r.duration / 1000));
return inSeconds.map((m) => `${m}s`).join(',');
}
function formatGroupedByResults(testAndResults: Array<TestAndResult>) {
const allPassed = testAndResults.every((m) => m.result.status === 'passed');
const allFailed = testAndResults.every((m) => m.result.status !== 'passed');
const firstItem = testAndResults[0]; // we know they all have the same state
const statuses = testAndResults.map((m) => `"${m.result.status}"`).join(',');
const times =
testAndResults.length === 1
? 'once'
: testAndResults.length === 2
? 'twice'
: `${testAndResults.length} times`;
console.log(
`${getChalkColorForStatus(
allPassed
? { status: 'passed' }
: allFailed
? { status: 'failed' }
: { status: 'interrupted' },
)(
`\t\t\t"${
firstItem.test.title
}": run ${times}, statuses:[${statuses}], durations: [${testResultToDurationStr(
testAndResults,
)}]`,
)}`,
);
}
class SessionReporter implements Reporter {
private printTestConsole = false;
private startTime: number = 0;
private allTestsCount: number = 0;
private allResults: Array<TestAndResult> = [];
private countWorkers: number = 1;
onBegin(config: FullConfig, suite: Suite) {
this.allTestsCount = suite.allTests().length;
this.countWorkers = config.workers;
this.printTestConsole = this.allTestsCount <= 1;
console.log(
`\t\tStarting the run with ${this.allTestsCount} tests, with ${this.countWorkers} workers, ${config.projects[0].retries} retries and ${config.projects[0].repeatEach} repeats`,
);
this.startTime = Date.now();
}
onTestBegin(test: TestCase, result: TestResult) {
console.log(
chalk.magenta(
`\tStarting test "${test.title}" ` +
`${result.retry > 0 ? `Retry #${test.retries}` : ''}`,
),
);
}
onTestEnd(test: TestCase, result: TestResult) {
if (result.status !== 'passed') {
console.log(
`${getChalkColorForStatus(result)(
`\t\tFinished test "${test.title}": ${result.status} with stdout/stderr`,
)}`,
);
console.warn(`stdout:`);
result.stdout.map((t) => process.stdout.write(t.toString()));
console.warn('stderr:');
result.stderr.map((t) => process.stderr.write(t.toString()));
} else {
console.log(
`${getChalkColorForStatus(result)(
`\t\tFinished test "${test.title}": ${result.status}`,
)}`,
);
}
this.allResults.push({ test, result });
console.log(chalk.bgWhiteBright(`\t\tResults so far:`));
// we keep track of all the failed/passed states, but only render the passed status here even if it took a few retries
const { allFailedSoFar, allPassedSoFar, partiallyPassed } =
this.groupResultsByTestName();
sortByTitle(allPassedSoFar).forEach((m) => formatGroupedByResults(m));
sortByTitle(partiallyPassed).forEach((m) => formatGroupedByResults(m));
sortByTitle(allFailedSoFar).forEach((m) => formatGroupedByResults(m));
const notPassedCount =
this.allTestsCount -
this.allResults.filter((m) => m.result.status === 'passed').length;
const estimateLeftMs =
notPassedCount * mean(this.allResults.map((m) => m.result.duration));
const estimatedTotalMins = Math.floor(estimateLeftMs / (60 * 1000));
console.log(
chalk.bgWhite(
`\t\tRemaining tests: ${notPassedCount}, rougly ${estimatedTotalMins}min total left, so about ${Math.ceil(
estimatedTotalMins / this.countWorkers,
)}min as we have ${this.countWorkers} worker(s)...`,
),
);
}
private groupResultsByTestName() {
const groupedByTitle = groupBy(this.allResults, (a) => a.test.title);
const allKeysPassedSoFar = Object.keys(groupedByTitle).filter((k) => {
return groupedByTitle[k].every((m) => m.result.status === 'passed');
});
const keysPartiallyPassedAndFailedSoFar = Object.keys(
groupedByTitle,
).filter((k) => {
return (
groupedByTitle[k].some((m) => m.result.status !== 'passed') &&
groupedByTitle[k].some((m) => m.result.status === 'passed')
);
});
const allKeysFailedSoFar = Object.keys(groupedByTitle).filter((k) => {
return groupedByTitle[k].every((m) => m.result.status !== 'passed');
});
return {
allPassedSoFar: groupBy(
this.allResults.filter((m) =>
allKeysPassedSoFar.includes(m.test.title),
),
(m) => m.test.title,
),
allFailedSoFar: groupBy(
this.allResults.filter((m) =>
allKeysFailedSoFar.includes(m.test.title),
),
(m) => m.test.title,
),
partiallyPassed: groupBy(
this.allResults.filter((m) =>
keysPartiallyPassedAndFailedSoFar.includes(m.test.title),
),
(m) => m.test.title,
),
};
}
onEnd(result: FullResult) {
console.log(
chalk.bgWhiteBright(
`\n\n\n\t\tFinished the run: ${result.status}, count of tests run: ${
this.allResults.length
}, took ${Math.floor(
(Date.now() - this.startTime) / (60 * 1000),
)} minute(s)`,
),
);
const { allFailedSoFar, allPassedSoFar, partiallyPassed } =
this.groupResultsByTestName();
sortByTitle(allPassedSoFar).forEach((m) => formatGroupedByResults(m));
sortByTitle(partiallyPassed).forEach((m) => formatGroupedByResults(m));
sortByTitle(allFailedSoFar).forEach((m) => formatGroupedByResults(m));
}
onStdOut?(
chunk: string | Buffer,
test: void | TestCase,
_result: void | TestResult,
) {
if (this.printTestConsole) {
process.stdout.write(
`"${test ? `${chalk.cyanBright(test.title)}` : ''}": ${chunk}`,
);
}
}
onError?(error: TestError) {
console.warn('global error:', error);
}
}
export default SessionReporter;