-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
188 lines (163 loc) · 4.86 KB
/
code.ts
File metadata and controls
188 lines (163 loc) · 4.86 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @fileoverview Code coverage utilities for parsing v8 coverage data.
*/
import process from 'node:process'
import { readJson } from '../fs'
import { isObjectObject } from '../objects'
import { spawn } from '../spawn'
import type {
CodeCoverageResult,
CoverageMetric,
GetCodeCoverageOptions,
V8CoverageData,
V8FileCoverage,
} from './types'
let _fs: typeof import('node:fs') | undefined
let _path: typeof import('node:path') | undefined
/**
* Calculate coverage metric with percentage.
*/
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,
}
}
/**
* Lazily load the fs module to avoid Webpack errors.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getFs() {
if (_fs === undefined) {
// Use non-'node:' prefixed require to avoid Webpack errors.
_fs = /*@__PURE__*/ require('node:fs')
}
return _fs as typeof import('node:fs')
}
/**
* Lazily load the path module to avoid Webpack errors.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getPath() {
if (_path === undefined) {
// Use non-'node:' prefixed require to avoid Webpack errors.
_path = /*@__PURE__*/ require('node:path')
}
return _path as typeof import('node:path')
}
/**
* 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 = getPath()
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 Error('Coverage path is required')
}
// Check if coverage file exists.
const fs = getFs()
if (!fs.existsSync(coveragePath)) {
if (generateIfMissing) {
// Run vitest to generate coverage.
await spawn('vitest', ['run', '--coverage'], {
cwd: process.cwd(),
stdio: 'inherit',
})
} else {
throw new Error(
`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 (!isObjectObject(coverageData)) {
throw new Error(`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 Object.values(v8Data)) {
if (!isObjectObject(fileCoverage)) {
continue
}
const fc = fileCoverage as V8FileCoverage
// Aggregate statements.
if (fc.s && isObjectObject(fc.s)) {
const statementCounts = Object.values(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 && isObjectObject(fc.b)) {
const branchCounts = Object.values(fc.b)
for (const branches of branchCounts) {
if (Array.isArray(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 && isObjectObject(fc.f)) {
const functionCounts = Object.values(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),
}
}