Skip to content

Commit 54c4b2d

Browse files
committed
refactor: better e2e test reporting
1 parent 4240200 commit 54c4b2d

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

site/e2e/reporter.ts

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from "fs";
1+
import * as fs from "fs";
22
import type {
33
FullConfig,
44
Suite,
@@ -8,52 +8,85 @@ import type {
88
Reporter,
99
} from "@playwright/test/reporter";
1010
import axios from "axios";
11+
import type { Writable } from "stream";
12+
13+
const testOutput = new Map<string, Array<[Writable, string]>>();
1114

1215
class CoderReporter implements Reporter {
1316
onBegin(config: FullConfig, suite: Suite) {
1417
// eslint-disable-next-line no-console -- Helpful for debugging
15-
console.log(`Starting the run with ${suite.allTests().length} tests`);
18+
console.log(`==> Running ${suite.allTests().length} tests`);
1619
}
1720

1821
onTestBegin(test: TestCase) {
22+
testOutput.set(test.id, []);
1923
// eslint-disable-next-line no-console -- Helpful for debugging
20-
console.log(`Starting test ${test.title}`);
24+
console.log(`==> Starting test ${test.title}`);
2125
}
2226

23-
onStdOut(chunk: string, test: TestCase, _: TestResult): void {
24-
// eslint-disable-next-line no-console -- Helpful for debugging
25-
console.log(
26-
`[stdout] [${test ? test.title : "unknown"}]: ${chunk.replace(
27-
/\n$/g,
28-
"",
29-
)}`,
30-
);
27+
onStdOut(chunk: string, test?: TestCase, _?: TestResult): void {
28+
if (!test) {
29+
// console.log(`[stdout] [unknown] ${chunk.replace(/\n$/g, "")}`);
30+
return;
31+
}
32+
testOutput.get(test.id)!.push([process.stdout, chunk]);
3133
}
3234

33-
onStdErr(chunk: string, test: TestCase, _: TestResult): void {
34-
// eslint-disable-next-line no-console -- Helpful for debugging
35-
console.log(
36-
`[stderr] [${test ? test.title : "unknown"}]: ${chunk.replace(
37-
/\n$/g,
38-
"",
39-
)}`,
40-
);
35+
onStdErr(chunk: string, test?: TestCase, _?: TestResult): void {
36+
if (!test) {
37+
// console.error(`[stderr] [unknown] ${chunk.replace(/\n$/g, "")}`);
38+
return;
39+
}
40+
testOutput.get(test.id)!.push([process.stderr, chunk]);
4141
}
4242

4343
async onTestEnd(test: TestCase, result: TestResult) {
4444
// eslint-disable-next-line no-console -- Helpful for debugging
45-
console.log(`Finished test ${test.title}: ${result.status}`);
45+
console.log(`==> Finished test ${test.title}: ${result.status}`);
4646

4747
if (result.status !== "passed") {
48-
// eslint-disable-next-line no-console -- Helpful for debugging
49-
console.log("errors", result.errors, "attachments", result.attachments);
48+
// eslint-disable-next-line no-console -- Debugging output
49+
console.log("==> Output");
50+
const output = testOutput.get(test.id)!;
51+
for (const [target, chunk] of output) {
52+
target.write(`${chunk.replace(/\n$/g, "")}\n`);
53+
}
54+
testOutput.delete(test.id);
55+
56+
if (result.errors.length > 0) {
57+
// eslint-disable-next-line no-console -- Debugging output
58+
console.log("==> Errors");
59+
for (const error of result.errors) {
60+
if (error.location) {
61+
console.log(`${error.location.file}:${error.location.line}:`);
62+
}
63+
if (error.snippet) {
64+
console.log(error.snippet);
65+
}
66+
67+
if (error.message) {
68+
console.log(error.message);
69+
} else {
70+
console.log(error);
71+
}
72+
}
73+
}
74+
75+
if (result.attachments.length > 0) {
76+
// eslint-disable-next-line no-console -- Debugging output
77+
console.log("==> Attachments");
78+
for (const attachment of result.attachments) {
79+
// eslint-disable-next-line no-console -- Debugging output
80+
console.log(attachment);
81+
}
82+
}
5083
}
5184
await exportDebugPprof(test.title);
5285
}
5386

5487
onEnd(result: FullResult) {
5588
// eslint-disable-next-line no-console -- Helpful for debugging
56-
console.log(`Finished the run: ${result.status}`);
89+
console.log(`==> Tests ${result.status}`);
5790
}
5891
}
5992

0 commit comments

Comments
 (0)