Skip to content

Commit eb0278a

Browse files
committed
Add --summary flag support to coverage script
Add --summary flag to hide detailed test output and coverage tables, showing only the final coverage summary. Add console.log blank line after header for consistent formatting. Change header text from "Running Coverage" to "Test Coverage" for consistency. Filter custom flags from vitest arguments to prevent unknown option errors.
1 parent 3ca67ea commit eb0278a

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

scripts/cover.mjs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* --open Open coverage report in browser
1212
* --code-only Run only code coverage (skip type coverage)
1313
* --type-only Run only type coverage (skip code coverage)
14+
* --summary Show only coverage summary (hide detailed output)
1415
*/
1516

1617
import { parseArgs } from 'node:util'
@@ -53,17 +54,27 @@ async function main() {
5354
options: {
5455
'code-only': { type: 'boolean', default: false },
5556
'type-only': { type: 'boolean', default: false },
57+
summary: { type: 'boolean', default: false },
5658
},
5759
strict: false,
5860
})
5961

6062
try {
6163
if (!quiet) {
62-
printHeader('Running Coverage')
64+
printHeader('Test Coverage')
65+
console.log('')
6366
}
6467

6568
// Run vitest with coverage enabled, capturing output
66-
const vitestArgs = ['exec', 'vitest', 'run', '--coverage']
69+
// Filter out custom flags that vitest doesn't understand
70+
const customFlags = ['--code-only', '--type-only', '--summary']
71+
const vitestArgs = [
72+
'exec',
73+
'vitest',
74+
'run',
75+
'--coverage',
76+
...process.argv.slice(2).filter(arg => !customFlags.includes(arg)),
77+
]
6778
const typeCoverageArgs = ['exec', 'type-coverage']
6879

6980
let exitCode = 0
@@ -95,9 +106,13 @@ async function main() {
95106
}
96107

97108
if (exitCode === 0) {
98-
if (!quiet) {printSuccess('Coverage completed successfully')}
109+
if (!quiet) {
110+
printSuccess('Coverage completed successfully')
111+
}
99112
} else {
100-
if (!quiet) {printError('Coverage failed')}
113+
if (!quiet) {
114+
printError('Coverage failed')
115+
}
101116
process.exitCode = 1
102117
}
103118
return
@@ -123,7 +138,7 @@ async function main() {
123138
const testSummaryMatch = output.match(
124139
/Test Files\s+\d+[^\n]*\n[\s\S]*?Duration\s+[\d.]+m?s[^\n]*/,
125140
)
126-
if (testSummaryMatch) {
141+
if (!values.summary && testSummaryMatch) {
127142
console.log()
128143
console.log(testSummaryMatch[0])
129144
console.log()
@@ -138,13 +153,15 @@ async function main() {
138153
)
139154

140155
if (coverageHeaderMatch && allFilesMatch) {
141-
console.log(' % Coverage report from v8')
142-
console.log(coverageHeaderMatch[1])
143-
console.log(coverageHeaderMatch[2])
144-
console.log(coverageHeaderMatch[1])
145-
console.log(allFilesMatch[0])
146-
console.log(coverageHeaderMatch[1])
147-
console.log()
156+
if (!values.summary) {
157+
console.log(' % Coverage report from v8')
158+
console.log(coverageHeaderMatch[1])
159+
console.log(coverageHeaderMatch[2])
160+
console.log(coverageHeaderMatch[1])
161+
console.log(allFilesMatch[0])
162+
console.log(coverageHeaderMatch[1])
163+
console.log()
164+
}
148165

149166
const codeCoveragePercent = Number.parseFloat(allFilesMatch[1])
150167
console.log(' Coverage Summary')
@@ -158,9 +175,13 @@ async function main() {
158175
}
159176

160177
if (exitCode === 0) {
161-
if (!quiet) {printSuccess('Coverage completed successfully')}
178+
if (!quiet) {
179+
printSuccess('Coverage completed successfully')
180+
}
162181
} else {
163-
if (!quiet) {printError('Coverage failed')}
182+
if (!quiet) {
183+
printError('Coverage failed')
184+
}
164185
process.exitCode = 1
165186
}
166187
return
@@ -202,20 +223,22 @@ async function main() {
202223

203224
// Display clean output
204225
if (!quiet) {
205-
if (testSummaryMatch) {
226+
if (!values.summary && testSummaryMatch) {
206227
console.log()
207228
console.log(testSummaryMatch[0])
208229
console.log()
209230
}
210231

211232
if (coverageHeaderMatch && allFilesMatch) {
212-
console.log(' % Coverage report from v8')
213-
console.log(coverageHeaderMatch[1]) // Top border
214-
console.log(coverageHeaderMatch[2]) // Header row
215-
console.log(coverageHeaderMatch[1]) // Middle border
216-
console.log(allFilesMatch[0]) // All files row
217-
console.log(coverageHeaderMatch[1]) // Bottom border
218-
console.log()
233+
if (!values.summary) {
234+
console.log(' % Coverage report from v8')
235+
console.log(coverageHeaderMatch[1]) // Top border
236+
console.log(coverageHeaderMatch[2]) // Header row
237+
console.log(coverageHeaderMatch[1]) // Middle border
238+
console.log(allFilesMatch[0]) // All files row
239+
console.log(coverageHeaderMatch[1]) // Bottom border
240+
console.log()
241+
}
219242

220243
// Display type coverage and cumulative summary
221244
if (typeCoverageMatch) {

0 commit comments

Comments
 (0)