Skip to content

Commit

Permalink
feat: Add file coverage compare logic
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed Oct 1, 2024
1 parent 3edd4ea commit ba33784
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
47 changes: 47 additions & 0 deletions src/report/generateFileCoverageHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createMockReportNumbers,
} from "../types/JsonSummaryMockFactory";
import { generateFileCoverageHtml } from "./generateFileCoverageHtml";
import { icons } from "../icons";

const workspacePath = process.cwd();
describe("generateFileCoverageHtml()", () => {
Expand All @@ -30,9 +31,11 @@ describe("generateFileCoverageHtml()", () => {

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

const firstTableLine = getTableLine(1, html);
Expand All @@ -50,9 +53,11 @@ describe("generateFileCoverageHtml()", () => {

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

expect(getTableLine(1, html)).toContain("Changed Files");
Expand All @@ -69,9 +74,11 @@ describe("generateFileCoverageHtml()", () => {

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

expect(html).not.toContain("Unchanged Files");
Expand All @@ -84,9 +91,11 @@ describe("generateFileCoverageHtml()", () => {

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

expect(html).toContain("No changed files found.");
Expand All @@ -104,9 +113,11 @@ describe("generateFileCoverageHtml()", () => {

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

const tableLine = getTableLine(2, html);
Expand All @@ -132,9 +143,11 @@ describe("generateFileCoverageHtml()", () => {

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

const tableLine = getTableLine(2, html);
Expand All @@ -159,8 +172,10 @@ describe("generateFileCoverageHtml()", () => {
const html = generateFileCoverageHtml({
jsonSummary,
jsonFinal,
jsonSummaryCompare: undefined,
fileCoverageMode: FileCoverageMode.All,
pullChanges: [],
commitSHA: "test-sha",
});

const tableLine = getTableLine(2, html);
Expand All @@ -187,14 +202,46 @@ describe("generateFileCoverageHtml()", () => {

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

const tableLine = getTableLine(2, html);

expect(tableLine).toContain("#L2");
expect(tableLine).toContain("#L5-L6");
});

it("renders an equal sign for files without changes to covergage", () => {
const jsonSummary: JsonSummary = createMockJsonSummary({
"file1.ts": createMockCoverageReport(),
});

const jsonSummaryCompare: JsonSummary = createMockJsonSummary({
"file1.ts": createMockCoverageReport(),
});
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.equal);
const equalSignCount = (html.match(new RegExp(icons.equal, "g")) || [])
.length;
expect(equalSignCount).toBe(4);
});
});
25 changes: 20 additions & 5 deletions src/report/generateFileCoverageHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import * as path from "node:path";
import { oneLine } from "common-tags";
import { FileCoverageMode } from "../inputs/FileCoverageMode";
import type { JsonFinal } from "../types/JsonFinal";
import type { JsonSummary } from "../types/JsonSummary";
import type { CoverageReport, JsonSummary } from "../types/JsonSummary";
import { generateBlobFileUrl } from "./generateFileUrl";
import {
type LineRange,
getUncoveredLinesFromStatements,
} from "./getUncoveredLinesFromStatements";
import { icons } from "../icons";

type FileCoverageInputs = {
jsonSummary: JsonSummary;
jsonSummaryCompare: JsonSummary | undefined;
jsonFinal: JsonFinal;
fileCoverageMode: FileCoverageMode;
pullChanges: string[];
Expand All @@ -20,6 +22,7 @@ type FileCoverageInputs = {
const workspacePath = process.cwd();
const generateFileCoverageHtml = ({
jsonSummary,
jsonSummaryCompare,
jsonFinal,
fileCoverageMode,
pullChanges,
Expand All @@ -29,6 +32,9 @@ const generateFileCoverageHtml = ({

const formatFileLine = (filePath: string) => {
const coverageSummary = jsonSummary[filePath];
const coverageSummaryCompare = jsonSummaryCompare
? jsonSummaryCompare[filePath]
: undefined;
const lineCoverage = jsonFinal[filePath];

// LineCoverage might be empty if coverage-final.json was not provided.
Expand All @@ -41,10 +47,10 @@ const generateFileCoverageHtml = ({
return `
<tr>
<td align="left"><a href="${url}">${relativeFilePath}</a></td>
<td align="right">${coverageSummary.statements.pct}%</td>
<td align="right">${coverageSummary.branches.pct}%</td>
<td align="right">${coverageSummary.functions.pct}%</td>
<td align="right">${coverageSummary.lines.pct}%</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>
<td align="left">${createRangeURLs(uncoveredLines, url)}</td>
</tr>`;
};
Expand Down Expand Up @@ -96,6 +102,15 @@ const generateFileCoverageHtml = ({
`;
};

function generateCoverageCell(
summary: CoverageReport,
summaryCompare: CoverageReport | undefined,
field: keyof CoverageReport,
): string {
const icon = summaryCompare ? icons.equal : "";
return `<td align="right">${summary[field].pct}%<br/>${icon}</td>`;
}

function formatGroupLine(caption: string): string {
return `
<tr>
Expand Down

1 comment on commit ba33784

@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 70.96%
⬆️ +0.46%
594 / 837
🔵 Statements 70.96%
⬆️ +0.46%
594 / 837
🔵 Functions 87.5%
⬆️ +0.41%
28 / 32
🔵 Branches 95.94%
⬆️ +0.14%
142 / 148
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Changed Files
src/report/generateFileCoverageHtml.ts 100%
100%
100%
100%
Unchanged Files
src/icons.ts 100%
100%
100%
100%
src/index.ts 0%
0%
0%
0%
1-6, 10-15, 17-18, 23-24, 26-27, 29, 31-36, 38-52, 54-59, 61-70, 72, 74-76, 78-80, 82-84, 86-87, 89-117, 119-143, 145-152, 154-158, 160-166
src/octokit.ts 0%
0%
0%
0%
1-2, 6-9
src/writeSummaryToComment.ts 100%
100%
100%
100%
src/writeSummaryToPR.ts 100%
100%
100%
100%
src/inputs/FileCoverageMode.ts 100%
100%
100%
100%
src/inputs/getCommentOn.ts 100%
100%
100%
100%
src/inputs/getCommitSHA.ts 100%
100%
100%
100%
src/inputs/getPullChanges.ts 100%
92.85%
100%
100%
src/inputs/getPullRequestNumber.ts 100%
100%
100%
100%
src/inputs/getViteConfigPath.ts 97.95%
87.5%
100%
97.95%
47
src/inputs/options.ts 0%
0%
0%
0%
1-2, 5-10, 26, 28, 30-31, 33-36, 38-41, 43-50, 52, 54, 57-60, 62-64, 66, 68-69, 71-72, 74-86
src/inputs/parseCoverageThresholds.ts 100%
100%
100%
100%
src/inputs/parseJsonReports.ts 0%
0%
0%
0%
1-4, 8-14, 16-24, 28-29, 32-34, 36-44, 48-52
src/report/generateCommitSHAUrl.ts 100%
100%
100%
100%
src/report/generateFileUrl.ts 100%
100%
100%
100%
src/report/generateHeadline.ts 100%
100%
100%
100%
src/report/generateSummaryTableHtml.ts 100%
100%
100%
100%
src/report/getUncoveredLinesFromStatements.ts 100%
100%
100%
100%
Generated in workflow #753 for commit ba33784 by the Vitest Coverage Report Action

Please sign in to comment.