-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
166 lines (142 loc) · 4.44 KB
/
code.ts
File metadata and controls
166 lines (142 loc) · 4.44 KB
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
/**
* @fileoverview Code coverage utilities for parsing v8 coverage data.
*/
import process from 'node:process'
import { readJson } from '../fs/read-json'
import { isPlainObject } from '../objects/predicates'
import { spawn } from '../spawn/spawn'
import { ArrayIsArray } from '../primordials/array'
import { ErrorCtor } from '../primordials/error'
import { ObjectValues } from '../primordials/object'
import { getNodeFs } from '../node/fs'
import { getNodePath } from '../node/path'
import type {
CodeCoverageResult,
CoverageMetric,
GetCodeCoverageOptions,
V8CoverageData,
V8FileCoverage,
} from './types'
/**
* Calculate coverage metric with percentage.
*/
export function calculateMetric(data: {
covered: number
total: number
}): CoverageMetric {
const percent =
data.total === 0 ? '0.00' : ((data.covered / data.total) * 100).toFixed(2)
return {
covered: data.covered,
percent,
total: data.total,
}
}
/**
* Get code coverage metrics from v8 coverage-final.json.
*
* @throws {Error} When coverage file doesn't exist and generateIfMissing is false.
* @throws {Error} When coverage data format is invalid.
*/
export async function getCodeCoverage(
options?: GetCodeCoverageOptions | undefined,
): Promise<CodeCoverageResult> {
const path = getNodePath()
const opts = {
__proto__: null,
coveragePath: path.join(process.cwd(), 'coverage/coverage-final.json'),
generateIfMissing: false,
...options,
} as GetCodeCoverageOptions
const { coveragePath, generateIfMissing } = opts
if (!coveragePath) {
throw new ErrorCtor('Coverage path is required')
}
// Check if coverage file exists.
const fs = getNodeFs()
if (!fs.existsSync(coveragePath)) {
if (generateIfMissing) {
// Run vitest to generate coverage.
await spawn('vitest', ['run', '--coverage'], {
cwd: process.cwd(),
stdio: 'inherit',
})
} else {
throw new ErrorCtor(
`Coverage file not found at "${coveragePath}". Run tests with coverage first.`,
)
}
}
// Read and parse coverage-final.json.
const coverageData = (await readJson(coveragePath)) as unknown
if (!isPlainObject(coverageData)) {
throw new ErrorCtor(`Invalid coverage data format in "${coveragePath}"`)
}
// Aggregate metrics across all files.
const totals = {
__proto__: null,
branches: { __proto__: null, covered: 0, total: 0 },
functions: { __proto__: null, covered: 0, total: 0 },
lines: { __proto__: null, covered: 0, total: 0 },
statements: { __proto__: null, covered: 0, total: 0 },
}
const v8Data = coverageData as V8CoverageData
for (const fileCoverage of ObjectValues(v8Data)) {
if (!isPlainObject(fileCoverage)) {
continue
}
const fc = fileCoverage as V8FileCoverage
// Aggregate statements.
if (fc.s && isPlainObject(fc.s)) {
const statementCounts = ObjectValues(fc.s)
for (const count of statementCounts) {
if (typeof count === 'number') {
totals.statements.total += 1
if (count > 0) {
totals.statements.covered += 1
}
}
}
}
// Aggregate branches.
if (fc.b && isPlainObject(fc.b)) {
const branchCounts = ObjectValues(fc.b)
for (const branches of branchCounts) {
if (ArrayIsArray(branches)) {
for (const count of branches) {
if (typeof count === 'number') {
totals.branches.total += 1
if (count > 0) {
totals.branches.covered += 1
}
}
}
}
}
}
// Aggregate functions.
if (fc.f && isPlainObject(fc.f)) {
const functionCounts = ObjectValues(fc.f)
for (const count of functionCounts) {
if (typeof count === 'number') {
totals.functions.total += 1
if (count > 0) {
totals.functions.covered += 1
}
}
}
}
// Note: Lines are typically derived from statement map in v8.
// For simplicity, we use statements as a proxy for lines.
// In a production implementation, you'd parse statementMap to get actual line coverage.
totals.lines.covered = totals.statements.covered
totals.lines.total = totals.statements.total
}
// Calculate percentages.
return {
branches: calculateMetric(totals.branches),
functions: calculateMetric(totals.functions),
lines: calculateMetric(totals.lines),
statements: calculateMetric(totals.statements),
}
}