-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-report.ts
More file actions
172 lines (155 loc) · 5.57 KB
/
Copy pathtest-report.ts
File metadata and controls
172 lines (155 loc) · 5.57 KB
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
import fs from 'node:fs/promises';
import { sendSlackMessage } from './slack.js';
import { getEnvVar } from './utils.js';
interface ReportTestResultsOptions {
reportFile: string;
dryRun: boolean;
reportSlackChannel?: string;
jobUrl?: string;
workflowName?: string;
}
export const reportTestResults = async ({
dryRun,
reportSlackChannel,
reportFile: jsonResultsPath,
jobUrl,
workflowName,
}: ReportTestResultsOptions) => {
const results: JsonTestResults = JSON.parse((await fs.readFile(jsonResultsPath)).toString());
const passed: JsonAssertionResult[] = [];
const failed: JsonAssertionResult[] = [];
for (const result of results.testResults) {
if (result.status !== 'failed') {
passed.push(...result.assertionResults);
continue;
}
for (const aResult of result.assertionResults) {
if (aResult.status !== 'failed') {
passed.push(aResult);
} else {
failed.push(aResult);
}
}
}
const failedAssertions: {
message: string;
runLink: string;
actorName: string;
alerts: JsonAssertionResult['meta']['alerts'];
}[] = [];
console.error();
console.error(`PASSED: ${passed.length}, FAILED: ${failed.length}`);
console.error();
console.error('**************************************************');
console.error('* Successes *');
console.error('**************************************************');
console.error();
for (const [i, aResult] of passed.entries()) {
const { fullName } = aResult;
console.error(`${i + 1}) ${fullName} ... ${aResult.meta.runLink}`);
console.error();
}
console.error('**************************************************');
console.error('* Failures *');
console.error('**************************************************');
console.error();
for (const [i, aResult] of failed.entries()) {
const { failureMessages, fullName, meta } = aResult;
if (failureMessages) {
failedAssertions.push(
...failureMessages.map((message) => ({
message: message.split('\n')?.[0],
runLink: meta.runLink,
actorName: meta.actorName,
alerts: meta.alerts,
})),
);
}
console.error(`${i + 1}) ${fullName} ... ${meta.runLink}`);
console.error();
}
console.error();
console.error(`PASSED: ${passed.length}, FAILED: ${failed.length}`);
console.error();
if (!reportSlackChannel) {
console.error(
`Skipping slack notification. If you want to enable it, add --report-slack-channel flag and make sure SLACK_TOKEN_TESTS_BOT env variable is set.`,
);
return;
}
// Default to true when alerts is not configured — backward-compatible
const slackAssertions = failedAssertions.filter(({ alerts }) => alerts?.slack !== false);
if (slackAssertions.length === 0) {
return;
}
// TODO: add slack profiles
const total = failed.length + passed.length;
const jobLink = jobUrl ? ` Check <${jobUrl}|the job>.` : '';
let slackMessage = `\`${workflowName ?? '-'}\``;
slackMessage += `: has ${slackAssertions.length} failed assertions. Failing test suites: ${failed.length}/${total}.${jobLink}`;
slackMessage += `\n\n${slackAssertions[0].message} --- <${slackAssertions[0].runLink}|${slackAssertions[0].actorName}>`;
const blocks = slackAssertions
.slice(1)
.map(({ message, runLink, actorName }) => `• ${message} --- <${runLink}|${actorName}>`);
console.error('SLACK:', slackMessage);
console.error('\tblocks:', blocks.join('\n\t\t'));
if (!dryRun) {
const slackToken = getEnvVar('SLACK_TOKEN_TESTS_BOT');
await sendSlackMessage(reportSlackChannel, slackMessage, blocks, slackToken);
}
};
type Status = 'passed' | 'failed' | 'skipped' | 'pending' | 'todo' | 'disabled';
type Milliseconds = number;
interface Callsite {
line: number;
column: number;
}
interface JsonAssertionResult {
ancestorTitles: string[];
fullName: string;
status: Status;
title: string;
meta: {
runId: string;
runLink: string;
actorName: string;
/**
* Alerting config set by the test via `alerts` in `testActor`/`describe`.
* `undefined` means the test didn't opt in or out — treat as "notify" for
* backward compatibility.
* `slack: false` explicitly disables the Slack notification for that test.
*/
alerts?: { slack?: boolean };
};
duration?: Milliseconds | null;
failureMessages: string[] | null;
location?: Callsite | null;
}
interface JsonTestResult {
message: string;
name: string;
status: 'failed' | 'passed';
startTime: number;
endTime: number;
assertionResults: JsonAssertionResult[];
// summary: string
// coverage: unknown
}
interface JsonTestResults {
numFailedTests: number;
numFailedTestSuites: number;
numPassedTests: number;
numPassedTestSuites: number;
numPendingTests: number;
numPendingTestSuites: number;
numTodoTests: number;
numTotalTests: number;
numTotalTestSuites: number;
startTime: number;
success: boolean;
testResults: JsonTestResult[];
// snapshot: SnapshotSummary
// coverageMap?: CoverageMap | null | undefined
// numRuntimeErrorTestSuites: number
// wasInterrupted: boolean
}