Skip to content

Commit

Permalink
feat: Enhance file coverage reporting with comparison metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed Nov 2, 2024
1 parent ba33784 commit 2a741d6
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { generateSummaryTableHtml } from "./report/generateSummaryTableHtml.js";
import type { JsonSummary } from "./types/JsonSummary.js";
import { writeSummaryToCommit } from "./writeSummaryToComment.js";
import { writeSummaryToPR } from "./writeSummaryToPR.js";
import { aw } from "vitest/dist/chunks/reporters.C_zwCd4j.js";

type GitHubSummary = typeof core.summary;

Expand Down Expand Up @@ -61,6 +60,7 @@ const run = async () => {
const jsonFinal = await parseVitestJsonFinal(options.jsonFinalPath);
const fileTable = generateFileCoverageHtml({
jsonSummary,
jsonSummaryCompare,
jsonFinal,
fileCoverageMode: options.fileCoverageMode,
pullChanges,
Expand Down
1 change: 0 additions & 1 deletion src/inputs/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { getPullRequestNumber } from "./getPullRequestNumber";
import { getViteConfigPath } from "./getViteConfigPath";
import { parseCoverageThresholds } from "./parseCoverageThresholds";
import { getCommentOn, type CommentOn } from "./getCommentOn";
import { report } from "node:process";

type Options = {
fileCoverageMode: FileCoverageMode;
Expand Down
68 changes: 68 additions & 0 deletions src/report/generateFileCoverageHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,72 @@ describe("generateFileCoverageHtml()", () => {
.length;
expect(equalSignCount).toBe(4);
});

it("renders a plus sign and the increased percentage for files with increased coverage", () => {
const jsonSummary: JsonSummary = createMockJsonSummary({
"file1.ts": createMockCoverageReport({
branches: createMockReportNumbers({ pct: 70 }),
}),
});

const jsonSummaryCompare: JsonSummary = createMockJsonSummary({
"file1.ts": createMockCoverageReport({
branches: createMockReportNumbers({ pct: 60 }),
}),
});
const jsonFinal: JsonFinal = {
...createJsonFinalEntry("src/exampleFile.ts", [
{ line: 1, covered: true },
]),
};

const html = generateFileCoverageHtml({
jsonSummary,
jsonSummaryCompare,
jsonFinal,
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
});

expect(html).toContain("file1.ts");
expect(html).toContain(`${icons.increase} <em>+10.00%</em>`);
const equalSignCount = (html.match(new RegExp(icons.increase, "g")) || [])
.length;
expect(equalSignCount).toBe(1);
});

it("renders a minus sign and the decreased percentage for files with decreased coverage", () => {
const jsonSummary: JsonSummary = createMockJsonSummary({
"file1.ts": createMockCoverageReport({
branches: createMockReportNumbers({ pct: 70 }),
}),
});

const jsonSummaryCompare: JsonSummary = createMockJsonSummary({
"file1.ts": createMockCoverageReport({
branches: createMockReportNumbers({ pct: 80 }),
}),
});
const jsonFinal: JsonFinal = {
...createJsonFinalEntry("src/exampleFile.ts", [
{ line: 1, covered: true },
]),
};

const html = generateFileCoverageHtml({
jsonSummary,
jsonSummaryCompare,
jsonFinal,
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
});

expect(html).toContain("file1.ts");
expect(html).toContain(`${icons.decrease} <em>-10.00%</em>`);
const equalSignCount = (html.match(new RegExp(icons.decrease, "g")) || [])
.length;
expect(equalSignCount).toBe(1);
});
});
17 changes: 11 additions & 6 deletions src/report/generateFileCoverageHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getUncoveredLinesFromStatements,
} from "./getUncoveredLinesFromStatements";
import { icons } from "../icons";
import { getCompareString } from "./getCompareString";

type FileCoverageInputs = {
jsonSummary: JsonSummary;
Expand Down Expand Up @@ -47,10 +48,10 @@ const generateFileCoverageHtml = ({
return `
<tr>
<td align="left"><a href="${url}">${relativeFilePath}</a></td>
<td align="right">${generateCoverageCell(coverageSummary, coverageSummaryCompare, "statements")}</td>
<td align="right">${generateCoverageCell(coverageSummary, coverageSummaryCompare, "branches")}</td>
<td align="right">${generateCoverageCell(coverageSummary, coverageSummaryCompare, "functions")}</td>
<td align="right">${generateCoverageCell(coverageSummary, coverageSummaryCompare, "lines")}</td>
${generateCoverageCell(coverageSummary, coverageSummaryCompare, "statements")}
${generateCoverageCell(coverageSummary, coverageSummaryCompare, "branches")}
${generateCoverageCell(coverageSummary, coverageSummaryCompare, "functions")}
${generateCoverageCell(coverageSummary, coverageSummaryCompare, "lines")}
<td align="left">${createRangeURLs(uncoveredLines, url)}</td>
</tr>`;
};
Expand Down Expand Up @@ -107,8 +108,12 @@ function generateCoverageCell(
summaryCompare: CoverageReport | undefined,
field: keyof CoverageReport,
): string {
const icon = summaryCompare ? icons.equal : "";
return `<td align="right">${summary[field].pct}%<br/>${icon}</td>`;
let diffText = "";
if (summaryCompare) {
const diff = summary[field].pct - summaryCompare[field].pct;
diffText = `<br/>${getCompareString(diff)}`;
}
return `<td align="right">${summary[field].pct}%${diffText}</td>`;
}

function formatGroupLine(caption: string): string {
Expand Down
14 changes: 1 addition & 13 deletions src/report/generateSummaryTableHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { oneLine } from "common-tags";
import { icons } from "../icons";
import type { CoverageReport, ReportNumbers } from "../types/JsonSummary";
import type { Thresholds } from "../types/Threshold";
import { getCompareString } from "./getCompareString";

function generateSummaryTableHtml(
jsonReport: CoverageReport,
Expand Down Expand Up @@ -69,17 +70,4 @@ function generateTableRow({
`;
}

function getCompareString(percentDiff: number): string {
if (percentDiff === 0) {
return `${icons.equal} <em>±0%</em>`;
}

if (percentDiff > 0) {
return `${icons.increase} <em>+${percentDiff.toFixed(2)}%</em>`;
}

// The - char is already included in a negative number
return `${icons.decrease} <em>${percentDiff.toFixed(2)}%</em>`;
}

export { generateSummaryTableHtml };
16 changes: 16 additions & 0 deletions src/report/getCompareString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { icons } from "../icons";

function getCompareString(percentDiff: number): string {
if (percentDiff === 0) {
return `${icons.equal} <em>±0%</em>`;
}

if (percentDiff > 0) {
return `${icons.increase} <em>+${percentDiff.toFixed(2)}%</em>`;
}

// The - char is already included in a negative number
return `${icons.decrease} <em>${percentDiff.toFixed(2)}%</em>`;
}

export { getCompareString };

1 comment on commit 2a741d6

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 71.09%
⬆️ +0.59%
600 / 844
🔵 Statements 71.09%
⬆️ +0.59%
600 / 844
🔵 Functions 87.5%
⬆️ +0.41%
28 / 32
🔵 Branches 95.91%
⬆️ +0.11%
141 / 147
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Changed Files
src/index.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
1-6, 10-15, 17-18, 22-23, 25-26, 28, 30-35, 37-51, 53-58, 60-70, 72, 74-76, 78-80, 82-84, 86-87, 89-117, 119-143, 145-152, 154-158, 160-166
src/inputs/options.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
1-2, 5-10, 25, 27, 29-30, 32-35, 37-40, 42-49, 51, 53, 56-59, 61-63, 65, 67-68, 70-71, 73-85
src/report/generateFileCoverageHtml.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/report/generateSummaryTableHtml.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/report/getCompareString.ts 100% 100% 100% 100%
Unchanged Files
src/icons.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/octokit.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
1-2, 6-9
src/writeSummaryToComment.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/writeSummaryToPR.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/FileCoverageMode.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/getCommentOn.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/getCommitSHA.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/getPullChanges.ts 100%
🟰 ±0%
92.85%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/getPullRequestNumber.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/getViteConfigPath.ts 97.95%
🟰 ±0%
87.5%
🟰 ±0%
100%
🟰 ±0%
97.95%
🟰 ±0%
47
src/inputs/parseCoverageThresholds.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/inputs/parseJsonReports.ts 0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
0%
🟰 ±0%
1-4, 8-14, 16-24, 28-29, 32-34, 36-44, 48-52
src/report/generateCommitSHAUrl.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/report/generateFileUrl.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/report/generateHeadline.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/report/getUncoveredLinesFromStatements.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
Generated in workflow #764 for commit 2a741d6 by the Vitest Coverage Report Action

Please sign in to comment.