Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 4x 2x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 1x 1x 4x 1x 5x 5x 4x 1x 1x 3x 3x 2x 2x 2x 1x 7x 7x 6x 1x 1x 1x 1x 1x 24x 20x 20x 16x 1x 7x 8x 8x 3x 5x 15x 7x 7x | import { access, cp, mkdir, readFile, rm } from "node:fs/promises";
import { resolve } from "node:path";
import type { CoverageArtifact } from "#layers/snapshot/shared/types/CoverageArtifact.ts";
import type { CoverageMetric } from "#layers/snapshot/shared/types/CoverageMetric.ts";
import type { ProjectCoverage } from "#layers/snapshot/shared/types/ProjectCoverage.ts";
import type { ProjectNode } from "#layers/snapshot/shared/types/ProjectNode.ts";
import { COVERAGE_DIR, MERGED_COVERAGE_DIR, WORKSPACE_ROOT } from "../lib/paths.ts";
interface RawSummary {
total: Record<string, CoverageMetric>;
[file: string]: unknown;
}
interface CoverageLocation {
start: { line: number };
}
interface CoverageFile {
statementMap: Record<string, CoverageLocation>;
s: Record<string, number>;
f: Record<string, number>;
b: Record<string, number[]>;
}
type RawCoverage = Record<string, CoverageFile>;
type SummaryReader = (root: string) => Promise<RawSummary | null>;
type Metric = "lines" | "statements" | "functions" | "branches";
const metric = (total: number, covered: number): CoverageMetric => ({
total,
covered,
pct: total === 0 ? 100 : Math.round((covered / total) * 10_000) / 100,
});
const summaryFromCoverage = (raw: RawCoverage): RawSummary | null => {
Iif (Object.keys(raw).length === 0) return null;
const files: Record<string, unknown> = {};
const lines = new Map<string, number>();
let statements = 0;
let coveredStatements = 0;
let functions = 0;
let coveredFunctions = 0;
let branches = 0;
let coveredBranches = 0;
for (const [path, file] of Object.entries(raw)) {
files[path] = file;
const statementCounts = Object.entries(file.s);
statements += statementCounts.length;
coveredStatements += statementCounts.filter(([, count]) => count > 0).length;
for (const [id, count] of statementCounts) {
const location = file.statementMap[id];
Iif (!location) continue;
const key = `${path}:${location.start.line}`;
lines.set(key, Math.max(lines.get(key) ?? 0, count));
}
const functionCounts = Object.values(file.f);
functions += functionCounts.length;
coveredFunctions += functionCounts.filter((count) => count > 0).length;
const branchCounts = Object.values(file.b).flat();
branches += branchCounts.length;
coveredBranches += branchCounts.filter((count) => count > 0).length;
}
const lineCounts = [...lines.values()];
return {
total: {
lines: metric(lineCounts.length, lineCounts.filter((count) => count > 0).length),
statements: metric(statements, coveredStatements),
functions: metric(functions, coveredFunctions),
branches: metric(branches, coveredBranches),
},
...files,
};
};
const readJson = async <T>(path: string): Promise<T | null> => {
try {
return JSON.parse(await readFile(path, "utf8")) as T;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
throw error;
}
};
/** Reads one project's summary, or derives one from its final coverage map. */
export const readSummary: SummaryReader = async (root) => {
const coverage = resolve(WORKSPACE_ROOT, root, "coverage");
const summary = await readJson<RawSummary>(resolve(coverage, "coverage-summary.json"));
Iif (summary) return summary;
const final = await readJson<RawCoverage>(resolve(coverage, "coverage-final.json"));
return final ? summaryFromCoverage(final) : null;
};
/** Copies in the report `pnpm test:coverage` already merges, which the app embeds as-is. */
const copyMergedReport = async (): Promise<boolean> => {
try {
await access(resolve(MERGED_COVERAGE_DIR, "index.html"));
} catch {
return false;
}
await rm(COVERAGE_DIR, { recursive: true, force: true });
await mkdir(COVERAGE_DIR, { recursive: true });
await cp(MERGED_COVERAGE_DIR, COVERAGE_DIR, { recursive: true });
return true;
};
const weighted = (projects: ProjectCoverage[], metric: Metric): number => {
const collected = projects.filter((project) => project[metric]);
const total = collected.reduce((sum, project) => sum + (project[metric]?.total ?? 0), 0);
const covered = collected.reduce((sum, project) => sum + (project[metric]?.covered ?? 0), 0);
return total === 0 ? 0 : Math.round((covered / total) * 10_000) / 100;
};
export const collectCoverage = async (
projects: ProjectNode[],
generatedAt: string,
read: SummaryReader = readSummary,
): Promise<CoverageArtifact> => {
const measured = await Promise.all(
projects.map(async (project): Promise<ProjectCoverage> => {
const summary = await read(project.root);
if (!summary)
return { name: project.name, root: project.root, collected: false, files: 0 };
return {
name: project.name,
root: project.root,
collected: true,
files: Object.keys(summary).filter((key) => key !== "total").length,
lines: summary.total.lines,
statements: summary.total.statements,
functions: summary.total.functions,
branches: summary.total.branches,
};
}),
);
const anyCollected = measured.some((project) => project.collected);
return {
generatedAt,
report: await copyMergedReport(),
totals: anyCollected
? {
lines: weighted(measured, "lines"),
statements: weighted(measured, "statements"),
functions: weighted(measured, "functions"),
branches: weighted(measured, "branches"),
}
: null,
projects: measured,
};
};
|