|
| 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