Skip to content

Commit 3425070

Browse files
committed
Added reporter class to add skipped test in .last-run.json file
1 parent de669dc commit 3425070

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/globals/global-setup.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { logDebug } from "@src/helper/logger/Logger";
2+
import { readdirSync, unlinkSync } from "fs";
3+
import { join } from "node:path";
4+
export default function globalSetup() {
5+
const logDir = "logs";
6+
try {
7+
const files = readdirSync(logDir).filter((file) =>
8+
file.endsWith(".log")
9+
);
10+
for (const file of files) {
11+
unlinkSync(join(logDir, file));
12+
}
13+
if (files.length > 0) {
14+
logDebug(
15+
`Deleted ${files.length} log file${files.length > 1 ? "s" : ""}.`
16+
);
17+
}
18+
} catch {}
19+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { Reporter, TestCase, TestResult } from "@playwright/test/reporter";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
import { logDebug, logError } from "@src/helper/logger/Logger";
5+
6+
interface FailedOrSkippedTest {
7+
file: string;
8+
line: number;
9+
title: string;
10+
}
11+
12+
class FailedAndSkippedReporter implements Reporter {
13+
private failedAndSkippedTests: FailedOrSkippedTest[] = [];
14+
private skippedTestIds: string[] = [];
15+
16+
onTestEnd(test: TestCase, result: TestResult) {
17+
if (result.status === "failed") {
18+
this.failedAndSkippedTests.push({
19+
file: test.location.file,
20+
line: test.location.line,
21+
title: test.titlePath().join(" > "),
22+
});
23+
}
24+
25+
if (result.status === "skipped") {
26+
this.failedAndSkippedTests.push({
27+
file: test.location.file,
28+
line: test.location.line,
29+
title: test.titlePath().join(" > "),
30+
});
31+
this.skippedTestIds.push(test.id);
32+
}
33+
}
34+
35+
onEnd() {
36+
if (this.failedAndSkippedTests.length === 0) {
37+
logDebug("No failed or skipped tests found.");
38+
return;
39+
}
40+
const lastFailedPath = path.resolve(
41+
process.cwd(),
42+
".last-failed-tests"
43+
);
44+
const lines = this.failedAndSkippedTests.map(
45+
(test) => `${test.file}:${test.line}:${test.title}`
46+
);
47+
48+
fs.writeFileSync(lastFailedPath, lines.join("\n"), "utf-8");
49+
logDebug(
50+
`\n[FailedAndSkippedReporter] Updated .last-failed-tests with ${lines.length} failed/skipped test(s).`
51+
);
52+
}
53+
54+
async onExit() {
55+
if (this.skippedTestIds.length === 0) {
56+
return;
57+
}
58+
59+
const lastRunPath = path.join(
60+
process.cwd(),
61+
"test-results",
62+
".last-run.json"
63+
);
64+
65+
if (!fs.existsSync(lastRunPath)) {
66+
return;
67+
}
68+
69+
try {
70+
const raw = fs.readFileSync(lastRunPath, "utf-8");
71+
const lastRunData: { status: string; failedTests: string[] } =
72+
JSON.parse(raw);
73+
const merged = new Set([
74+
...lastRunData.failedTests,
75+
...this.skippedTestIds,
76+
]);
77+
lastRunData.failedTests = [...merged];
78+
fs.writeFileSync(
79+
lastRunPath,
80+
JSON.stringify(lastRunData, null, 2),
81+
"utf-8"
82+
);
83+
logDebug(
84+
`[FailedAndSkippedReporter] merged ${this.skippedTestIds.length} skipped test(s) into .last-run.json (total failedTests: ${lastRunData.failedTests.length}.`
85+
);
86+
} catch (err) {
87+
logError(
88+
`[FailedAndSkippedReporter] Error occurred while updating .last-run.json: ${err}`
89+
);
90+
}
91+
}
92+
}
93+
94+
export default FailedAndSkippedReporter;

0 commit comments

Comments
 (0)