|
| 1 | +/* |
| 2 | + * Wire |
| 3 | + * Copyright (C) 2025 Wire Swiss GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +import type {JSONReport, JSONReportSuite, JSONReportTest} from '@playwright/test/reporter'; |
| 21 | + |
| 22 | +import {readFileSync, writeFileSync} from 'fs'; |
| 23 | +import {resolve} from 'path'; |
| 24 | + |
| 25 | +const jsonPath = resolve('playwright-report', 'report.json'); |
| 26 | +let report: JSONReport; |
| 27 | + |
| 28 | +try { |
| 29 | + report = JSON.parse(readFileSync(jsonPath, 'utf-8')); |
| 30 | +} catch (error) { |
| 31 | + const errorMessage = `❌ Error: report.json not found at ${jsonPath} ❌`; |
| 32 | + writeFileSync('playwright-report-summary.txt', errorMessage); |
| 33 | + process.exit(1); |
| 34 | +} |
| 35 | + |
| 36 | +const getTests = (suite: JSONReportSuite): (JSONReportTest & {file: string; title: string; tags: string[]})[] => { |
| 37 | + return [ |
| 38 | + ...(suite.specs.flatMap(spec => |
| 39 | + spec.tests.map(test => ({ |
| 40 | + ...test, |
| 41 | + file: spec.file, |
| 42 | + // If no title is provided the file would be used which is redundant |
| 43 | + title: spec.file !== suite.title ? `${suite.title} > ${spec.title}` : spec.title, |
| 44 | + tags: spec.tags, |
| 45 | + })), |
| 46 | + ) ?? []), |
| 47 | + ...(suite.suites?.flatMap(suite => getTests(suite)) ?? []), |
| 48 | + ]; |
| 49 | +}; |
| 50 | + |
| 51 | +const tests = report.suites.flatMap(suite => getTests(suite)); |
| 52 | +const failedOrFlakyTests = tests.filter(test => test.status === 'unexpected' || test.status === 'flaky'); |
| 53 | +const testFilesToReport = Object.groupBy(failedOrFlakyTests, test => test.file); |
| 54 | + |
| 55 | +const testDetails = Object.values(testFilesToReport).reduce((acc, testFile) => { |
| 56 | + if (!testFile?.length) { |
| 57 | + return acc; |
| 58 | + } |
| 59 | + |
| 60 | + const failedTests = testFile |
| 61 | + .filter(test => test.status === 'unexpected') |
| 62 | + .map(({title, tags}) => `❌ ${title} (tags: ${tags.join(', ')})`); |
| 63 | + |
| 64 | + const flakyTests = testFile |
| 65 | + .filter(test => test.status === 'flaky') |
| 66 | + .map(({title, tags}) => `⚠️ ${title} (tags: ${tags.join(', ')})`); |
| 67 | + |
| 68 | + acc += ` |
| 69 | +<details> |
| 70 | + <summary>${testFile[0].file} (❌ ${failedTests.length} failed, ⚠️ ${flakyTests.length} flaky)</summary> |
| 71 | +
|
| 72 | + ${[...failedTests, ...flakyTests].join('\n ')} |
| 73 | +</details> |
| 74 | +`; |
| 75 | + |
| 76 | + return acc; |
| 77 | +}, ''); |
| 78 | + |
| 79 | +const formatDuration = (duration: number) => { |
| 80 | + const minutes = Math.floor(duration / 60000); |
| 81 | + const totalSeconds = Math.round((duration % 60000) / 1000); |
| 82 | + const seconds = totalSeconds % 60; |
| 83 | + return `~ ${minutes} min ${seconds} sec`; |
| 84 | +}; |
| 85 | + |
| 86 | +const summary = ` |
| 87 | +### 🧪 Playwright Test Summary |
| 88 | +
|
| 89 | +- ✅ **Passed:** ${report.stats.expected} |
| 90 | +- ❌ **Failed:** ${report.stats.unexpected} |
| 91 | +- ⏭ **Skipped:** ${report.stats.skipped} |
| 92 | +- 🔁 **Flaky:** ${report.stats.flaky} |
| 93 | +- 📊 **Total:** ${report.stats.expected + report.stats.unexpected + report.stats.skipped + report.stats.flaky} |
| 94 | +- ⏱ **Total Runtime:** ${(report.stats.duration / 1000).toFixed(1)}s (${formatDuration(report.stats.duration)}) |
| 95 | +
|
| 96 | +${testDetails} |
| 97 | +`; |
| 98 | + |
| 99 | +writeFileSync('playwright-report-summary.txt', summary); |
0 commit comments