e2e-reporting-coverage.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type { UIRendererCoverageSummary } from "./e2e-ui-coverage.js";
  2. export function renderUICoverageMarkdownSection(
  3. coverage: UIRendererCoverageSummary,
  4. ) {
  5. const sourceFiles = coverage.sourceFiles ?? [];
  6. const lines = [
  7. "## Coverage",
  8. "",
  9. "### UI Renderer",
  10. "",
  11. `- Report kind: \`${coverage.reportKind ?? "bundle-byte"}\``,
  12. `- Pages: \`${coverage.pageCount}\``,
  13. `- Instrumented files: \`${coverage.rawEntryCount}\``,
  14. `- Source files: \`${coverage.sourceFileCount ?? sourceFiles.length}\``,
  15. `- Line coverage: \`${formatCoverageTotal(coverage.totals.lines)}\``,
  16. `- Statement coverage: \`${formatCoverageTotal(coverage.totals.statements)}\``,
  17. `- Function coverage: \`${formatCoverageTotal(coverage.totals.functions)}\``,
  18. `- Branch coverage: \`${formatCoverageTotal(coverage.totals.branches)}\``,
  19. ...(coverage.totals.totalBytes > 0
  20. ? [
  21. `- Bundle byte coverage: \`${coverage.totals.percent}% (${coverage.totals.coveredBytes}/${coverage.totals.totalBytes})\``,
  22. `- Renderer scripts: \`${coverage.rendererEntryCount}\``,
  23. ]
  24. : []),
  25. `- Istanbul HTML: \`${coverage.files.htmlIndex ?? "n/a"}\``,
  26. `- LCOV: \`${coverage.files.lcov ?? "n/a"}\``,
  27. `- Coverage JSON: \`${coverage.files.coverageFinalJson ?? "n/a"}\``,
  28. "",
  29. ];
  30. if (sourceFiles.length > 0) {
  31. lines.push(
  32. "| Lines | Statements | Functions | Branches | Uncovered lines | File |",
  33. "| ---: | ---: | ---: | ---: | --- | --- |",
  34. ...sourceFiles.map(
  35. (file) =>
  36. `| ${formatCoverageTotal(file.lines)} | ${formatCoverageTotal(file.statements)} | ${formatCoverageTotal(file.functions)} | ${formatCoverageTotal(file.branches)} | ${formatUncoveredLines(file.uncoveredLines)} | \`${escapeTableCell(file.relativePath)}\` |`,
  37. ),
  38. "",
  39. );
  40. return lines;
  41. }
  42. lines.push(
  43. "| Coverage | Covered bytes | Total bytes | Script |",
  44. "| ---: | ---: | ---: | --- |",
  45. ...coverage.scripts.map((script) => {
  46. const scriptName = script.url.split("/").at(-1) ?? script.url;
  47. return `| ${script.percent}% | ${script.coveredBytes} | ${script.totalBytes} | \`${escapeTableCell(scriptName)}\` |`;
  48. }),
  49. "",
  50. );
  51. return lines;
  52. }
  53. function formatCoverageTotal(
  54. total:
  55. | {
  56. covered: number;
  57. pct: number;
  58. total: number;
  59. }
  60. | undefined,
  61. ) {
  62. if (!total) {
  63. return "n/a";
  64. }
  65. return `${total.pct}% (${total.covered}/${total.total})`;
  66. }
  67. function formatUncoveredLines(lines: number[]) {
  68. if (lines.length === 0) {
  69. return "-";
  70. }
  71. if (lines.length <= 12) {
  72. return lines.join(", ");
  73. }
  74. return `${lines.slice(0, 12).join(", ")} ... +${lines.length - 12}`;
  75. }
  76. function escapeTableCell(value: string) {
  77. return value.replace(/\|/g, "\\|");
  78. }