|
| 1 | +import type { |
| 2 | + CoverageMap, |
| 3 | + CoverageSummary, |
| 4 | + CoverageThresholds, |
| 5 | +} from '../types/coverage'; |
| 6 | +import { color } from '../utils'; |
| 7 | + |
| 8 | +export function checkThresholds( |
| 9 | + coverageMap: CoverageMap, |
| 10 | + thresholds?: CoverageThresholds, |
| 11 | +): { success: boolean; message: string } { |
| 12 | + if (!thresholds) { |
| 13 | + return { success: true, message: '' }; |
| 14 | + } |
| 15 | + const summary = coverageMap.getCoverageSummary(); |
| 16 | + const failedThresholds: string[] = []; |
| 17 | + |
| 18 | + const check = ( |
| 19 | + name: keyof CoverageSummary, |
| 20 | + type: string, |
| 21 | + actual: number, |
| 22 | + expected?: number, |
| 23 | + ) => { |
| 24 | + if (expected !== undefined && actual < expected) { |
| 25 | + failedThresholds.push( |
| 26 | + `Coverage for ${name} ${color.red(`${actual}%`)} does not meet ${type} threshold ${color.yellow(`${expected}%`)}`, |
| 27 | + ); |
| 28 | + } |
| 29 | + }; |
| 30 | + // Check global thresholds |
| 31 | + check('statements', 'global', summary.statements.pct, thresholds.statements); |
| 32 | + check('functions', 'global', summary.functions.pct, thresholds.functions); |
| 33 | + check('branches', 'global', summary.branches.pct, thresholds.branches); |
| 34 | + check('lines', 'global', summary.lines.pct, thresholds.lines); |
| 35 | + |
| 36 | + return { |
| 37 | + success: failedThresholds.length === 0, |
| 38 | + message: failedThresholds.join('\n'), |
| 39 | + }; |
| 40 | +} |
0 commit comments