-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown-reporter.ts
More file actions
127 lines (105 loc) · 3.08 KB
/
markdown-reporter.ts
File metadata and controls
127 lines (105 loc) · 3.08 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
import fs from "fs";
import type {
FullResult,
Reporter,
TestCase,
TestResult,
TestStatus,
TestStep,
} from "@playwright/test/reporter";
class MarkdownReporter implements Reporter {
lines: string[];
outputFolder: string;
steps: string;
totalFailedTests: number;
totalPassedTests: number;
totalSkippedTests: number;
totalTests: number;
constructor(options: { outputFolder?: string } = {}) {
this.lines = [];
this.outputFolder = options.outputFolder || ".";
this.steps = "";
this.totalFailedTests = 0;
this.totalPassedTests = 0;
this.totalSkippedTests = 0;
this.totalTests = 0;
}
onStepEnd(test: TestCase, result: TestResult, step: TestStep) {
// @TODO report on pass/failure of steps
if (step.category === "test.step") {
this.steps += `- ${step.title}\n`;
}
}
onTestEnd(test: TestCase, result: TestResult) {
const resultStatus = result.status;
this.totalTests++;
if (resultStatus === "passed") {
this.totalPassedTests++;
}
if (resultStatus === "skipped") {
this.totalSkippedTests++;
}
if (["failed", "timedOut", "interrupted"].includes(resultStatus)) {
this.totalFailedTests++;
}
let testSummary =
"\n<details><summary><h2>" +
getSymbolByStatus(resultStatus) +
` (${resultStatus}) ` +
`${test.parent.title} > ${test.title}` +
"</h2></summary>\n" +
`<h3>${test.parent.parent?.title || ""}</h3>\n\n`;
if (this.steps !== "") {
testSummary += this.steps;
}
if (test.annotations.length) {
let isFirstIssueAnnotation = true;
testSummary += "\n<h3>Details</h3>\n\n";
for (const { type, description } of test.annotations) {
if (type === "issue") {
if (!isFirstIssueAnnotation) {
// Close previous issue annotation details
testSummary += `</details>`;
} else {
isFirstIssueAnnotation = false;
}
testSummary += `<details><summary><h4>${description}</h4></summary>\n\n`;
}
if (type === "issue-details") {
// Issue details that belong to the parent issue; relies on array order
testSummary += `- ${description}\n`;
}
}
// Close last issue annotation details
testSummary += "</details>\n";
}
// Close parent test summary details
testSummary += "\n</details>";
this.lines = [...this.lines, testSummary];
this.steps = "";
}
onEnd(result: FullResult) {
const markdownDoc = [
"# Test Summary",
`\nTotal tests: ${this.totalTests} | Passed: ${this.totalPassedTests} | Failed: ${this.totalFailedTests} | Skipped: ${this.totalSkippedTests}\n`,
...this.lines,
].join("\n");
fs.writeFileSync(
`${__dirname}/${this.outputFolder}/test-summary.md`,
markdownDoc,
);
}
}
function getSymbolByStatus(status: TestStatus) {
if (status === "skipped") {
return "⏭";
}
if (status === "passed") {
return "✅";
}
// "failed" | "timedOut" | "interrupted"
else {
return "❌";
}
}
export default MarkdownReporter;