Skip to content
Source
Sign inSign up
TanStack/table
Open History to see the last commit

coverage-summary.mjs

90 lines2.7 KB

1// Formats a vitest `json-summary` coverage report as GitHub-flavored markdown
2// for the Actions step summary. Report-only: no thresholds are enforced.
3//
4// Usage: node scripts/coverage-summary.mjs <path-to-coverage-summary.json> [title]
5​
6import fs from 'node:fs'
7import path from 'node:path'
8​
9const [, , summaryPath, title = 'Coverage Report'] = process.argv
10​
11if (!summaryPath || !fs.existsSync(summaryPath)) {
12 console.error(`coverage summary not found at: ${summaryPath}`)
13 process.exit(1)
15​
16const summary = JSON.parse(fs.readFileSync(summaryPath, 'utf8'))
17​
18const pct = (metric) => `${metric.pct}% (${metric.covered}/${metric.total})`
19​
20const row = (label, entry) =>
21 `| ${label} | ${pct(entry.statements)} | ${pct(entry.branches)} | ${pct(entry.functions)} | ${pct(entry.lines)} |`
22​
23// Aggregate per-file entries into per-directory groups under src/
24// (e.g. `core/rows`, `features/row-sorting`, `fns`).
25const groups = new Map()
26​
27for (const [filePath, entry] of Object.entries(summary)) {
28 if (filePath === 'total') continue
29​
30 const srcIndex = filePath.lastIndexOf(`${path.sep}src${path.sep}`)
31 const relative =
32 srcIndex === -1
33 ? path.basename(filePath)
34 : filePath.slice(srcIndex + `${path.sep}src${path.sep}`.length)
35 const segments = relative.split(path.sep)
36 const group =
37 segments.length > 2
38 ? segments.slice(0, 2).join('/')
39 : segments.length === 2
40 ? segments[0]
41 : '(root)'
42​
43 let acc = groups.get(group)
44 if (!acc) {
45 acc = {
46 statements: { covered: 0, total: 0 },
47 branches: { covered: 0, total: 0 },
48 functions: { covered: 0, total: 0 },
49 lines: { covered: 0, total: 0 },
50 }
51 groups.set(group, acc)
52 }
53 for (const metric of ['statements', 'branches', 'functions', 'lines']) {
54 acc[metric].covered += entry[metric].covered
55 acc[metric].total += entry[metric].total
56 }
58​
59const withPct = (acc) => {
60 const result = {}
61 for (const metric of ['statements', 'branches', 'functions', 'lines']) {
62 const { covered, total } = acc[metric]
63 result[metric] = {
64 covered,
65 total,
66 pct: total === 0 ? 100 : Math.round((covered / total) * 10000) / 100,
67 }
68 }
69 return result
71​
72const lines = [
73 `## ${title}`,
74 '',
75 '| | Statements | Branches | Functions | Lines |',
76 '| --- | --- | --- | --- | --- |',
77 row('**Total**', summary.total),
78 '',
79 '<details><summary>Per-directory breakdown</summary>',
80 '',
81 '| Directory | Statements | Branches | Functions | Lines |',
82 '| --- | --- | --- | --- | --- |',
83 ...[...groups.entries()]
84 .sort(([a], [b]) => a.localeCompare(b))
85 .map(([group, acc]) => row(`\`${group}\``, withPct(acc))),
86 '',
87 '</details>',
89​
90console.log(lines.join('\n'))