|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +const summaryPath = path.resolve(process.cwd(), 'coverage', 'coverage-summary.json'); |
| 5 | + |
| 6 | +function formatPercent(value) { |
| 7 | + return `${Number(value).toFixed(2)}%`; |
| 8 | +} |
| 9 | + |
| 10 | +function printBlock(title, metrics) { |
| 11 | + console.log(`\n${title}`); |
| 12 | + console.log(`- Statement coverage: ${formatPercent(metrics.statements.pct)}`); |
| 13 | + console.log(`- Branch coverage (if/switch): ${formatPercent(metrics.branches.pct)}`); |
| 14 | + console.log(`- Function coverage: ${formatPercent(metrics.functions.pct)}`); |
| 15 | + console.log(`- Line coverage: ${formatPercent(metrics.lines.pct)}`); |
| 16 | +} |
| 17 | + |
| 18 | +try { |
| 19 | + const raw = readFileSync(summaryPath, 'utf-8'); |
| 20 | + const summary = JSON.parse(raw); |
| 21 | + const total = summary.total; |
| 22 | + |
| 23 | + console.log('\n=== Coverage Summary ==='); |
| 24 | + printBlock('Whole project', total); |
| 25 | + |
| 26 | + console.log('\nCore logic details:'); |
| 27 | + Object.entries(summary) |
| 28 | + .filter(([key]) => key !== 'total') |
| 29 | + .forEach(([file, metrics]) => { |
| 30 | + printBlock(file, metrics); |
| 31 | + }); |
| 32 | + |
| 33 | + console.log('\nVitest table legend:'); |
| 34 | + console.log('- % Stmts = statement coverage'); |
| 35 | + console.log('- % Branch = branch coverage (if/else/switch)'); |
| 36 | + console.log('- % Funcs = function coverage'); |
| 37 | + console.log('- % Lines = line coverage'); |
| 38 | +} catch (error) { |
| 39 | + console.error('Failed to read coverage-summary.json.'); |
| 40 | + console.error(error instanceof Error ? error.message : String(error)); |
| 41 | + process.exit(1); |
| 42 | +} |
0 commit comments