-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcoverage.ts
644 lines (545 loc) · 19.1 KB
/
coverage.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import type { CoverageMap } from 'istanbul-lib-coverage'
import type { Vitest } from '../node/core'
import type { BaseCoverageOptions, ReportContext, ResolvedCoverageOptions } from '../node/types/coverage'
import type { AfterSuiteRunMeta } from '../types/general'
import { existsSync, promises as fs, readdirSync, writeFileSync } from 'node:fs'
import mm from 'micromatch'
import { relative, resolve } from 'pathe'
import c from 'tinyrainbow'
import { coverageConfigDefaults } from '../defaults'
import { resolveCoverageReporters } from '../node/config/resolveConfig'
type Threshold = 'lines' | 'functions' | 'statements' | 'branches'
interface ResolvedThreshold {
coverageMap: CoverageMap
name: string
thresholds: Partial<Record<Threshold, number | undefined>>
}
/**
* Holds info about raw coverage results that are stored on file system:
*
* ```json
* "project-a": {
* "web": {
* "tests/math.test.ts": "coverage-1.json",
* "tests/utils.test.ts": "coverage-2.json",
* // ^^^^^^^^^^^^^^^ Raw coverage on file system
* },
* "ssr": { ... },
* "browser": { ... },
* },
* "project-b": ...
* ```
*/
type CoverageFiles = Map<
NonNullable<AfterSuiteRunMeta['projectName']> | symbol,
Record<
AfterSuiteRunMeta['transformMode'],
{ [TestFilenames: string]: string }
>
>
type Entries<T> = [keyof T, T[keyof T]][]
const THRESHOLD_KEYS: Readonly<Threshold[]> = [
'lines',
'functions',
'statements',
'branches',
]
const GLOBAL_THRESHOLDS_KEY = 'global'
const DEFAULT_PROJECT: unique symbol = Symbol.for('default-project')
let uniqueId = 0
export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istanbul' | 'v8'>> {
ctx!: Vitest
readonly name!: 'v8' | 'istanbul'
version!: string
options!: Options
coverageFiles: CoverageFiles = new Map()
pendingPromises: Promise<void>[] = []
coverageFilesDirectory!: string
_initialize(ctx: Vitest) {
this.ctx = ctx
if (ctx.version !== this.version) {
ctx.logger.warn(
c.yellow(
`Loaded ${c.inverse(c.yellow(` vitest@${ctx.version} `))} and ${c.inverse(c.yellow(` @vitest/coverage-${this.name}@${this.version} `))}.`
+ '\nRunning mixed versions is not supported and may lead into bugs'
+ '\nUpdate your dependencies and make sure the versions match.',
),
)
}
const config = ctx.config.coverage as Options
this.options = {
...coverageConfigDefaults,
// User's options
...config,
// Resolved fields
provider: this.name,
reportsDirectory: resolve(
ctx.config.root,
config.reportsDirectory || coverageConfigDefaults.reportsDirectory,
),
reporter: resolveCoverageReporters(
config.reporter || coverageConfigDefaults.reporter,
),
thresholds: config.thresholds && {
...config.thresholds,
lines: config.thresholds['100'] ? 100 : config.thresholds.lines,
branches: config.thresholds['100'] ? 100 : config.thresholds.branches,
functions: config.thresholds['100'] ? 100 : config.thresholds.functions,
statements: config.thresholds['100'] ? 100 : config.thresholds.statements,
},
}
const shard = this.ctx.config.shard
const tempDirectory = `.tmp${
shard ? `-${shard.index}-${shard.count}` : ''
}`
this.coverageFilesDirectory = resolve(
this.options.reportsDirectory,
tempDirectory,
)
}
createCoverageMap(): CoverageMap {
throw new Error('BaseReporter\'s createCoverageMap was not overwritten')
}
async generateReports(_: CoverageMap, __: boolean | undefined) {
throw new Error('BaseReporter\'s generateReports was not overwritten')
}
async parseConfigModule(_: string): Promise<{ generate: () => { code: string } }> {
throw new Error('BaseReporter\'s parseConfigModule was not overwritten')
}
resolveOptions() {
return this.options
}
async clean(clean = true): Promise<void> {
if (clean && existsSync(this.options.reportsDirectory)) {
await fs.rm(this.options.reportsDirectory, {
recursive: true,
force: true,
maxRetries: 10,
})
}
if (existsSync(this.coverageFilesDirectory)) {
await fs.rm(this.coverageFilesDirectory, {
recursive: true,
force: true,
maxRetries: 10,
})
}
await fs.mkdir(this.coverageFilesDirectory, { recursive: true })
this.coverageFiles = new Map()
this.pendingPromises = []
}
onAfterSuiteRun({ coverage, transformMode, projectName, testFiles }: AfterSuiteRunMeta): void {
if (!coverage) {
return
}
if (transformMode !== 'web' && transformMode !== 'ssr' && transformMode !== 'browser') {
throw new Error(`Invalid transform mode: ${transformMode}`)
}
let entry = this.coverageFiles.get(projectName || DEFAULT_PROJECT)
if (!entry) {
entry = { web: {}, ssr: {}, browser: {} }
this.coverageFiles.set(projectName || DEFAULT_PROJECT, entry)
}
const testFilenames = testFiles.join()
const filename = resolve(
this.coverageFilesDirectory,
`coverage-${uniqueId++}.json`,
)
// If there's a result from previous run, overwrite it
entry[transformMode][testFilenames] = filename
const promise = fs.writeFile(filename, JSON.stringify(coverage), 'utf-8')
this.pendingPromises.push(promise)
}
async readCoverageFiles<CoverageType>({ onFileRead, onFinished, onDebug }: {
/** Callback invoked with a single coverage result */
onFileRead: (data: CoverageType) => void
/** Callback invoked once all results of a project for specific transform mode are read */
onFinished: (project: Vitest['projects'][number], transformMode: AfterSuiteRunMeta['transformMode']) => Promise<void>
onDebug: ((...logs: any[]) => void) & { enabled: boolean }
}) {
let index = 0
const total = this.pendingPromises.length
await Promise.all(this.pendingPromises)
this.pendingPromises = []
for (const [projectName, coveragePerProject] of this.coverageFiles.entries()) {
for (const [transformMode, coverageByTestfiles] of Object.entries(coveragePerProject) as Entries<typeof coveragePerProject>) {
const filenames = Object.values(coverageByTestfiles)
const project = this.ctx.getProjectByName(projectName as string)
for (const chunk of this.toSlices(filenames, this.options.processingConcurrency)) {
if (onDebug.enabled) {
index += chunk.length
onDebug('Covered files %d/%d', index, total)
}
await Promise.all(chunk.map(async (filename) => {
const contents = await fs.readFile(filename, 'utf-8')
const coverage = JSON.parse(contents)
onFileRead(coverage)
}),
)
}
await onFinished(project, transformMode)
}
}
}
async cleanAfterRun() {
this.coverageFiles = new Map()
await fs.rm(this.coverageFilesDirectory, { recursive: true })
// Remove empty reports directory, e.g. when only text-reporter is used
if (readdirSync(this.options.reportsDirectory).length === 0) {
await fs.rm(this.options.reportsDirectory, { recursive: true })
}
}
async onTestFailure() {
if (!this.options.reportOnFailure) {
await this.cleanAfterRun()
}
}
async reportCoverage(coverageMap: unknown, { allTestsRun }: ReportContext): Promise<void> {
await this.generateReports(
(coverageMap as CoverageMap) || this.createCoverageMap(),
allTestsRun,
)
// In watch mode we need to preserve the previous results if cleanOnRerun is disabled
const keepResults = !this.options.cleanOnRerun && this.ctx.config.watch
if (!keepResults) {
await this.cleanAfterRun()
}
}
async reportThresholds(coverageMap: CoverageMap, allTestsRun: boolean | undefined) {
const resolvedThresholds = this.resolveThresholds(coverageMap)
this.checkThresholds(resolvedThresholds)
if (this.options.thresholds?.autoUpdate && allTestsRun) {
if (!this.ctx.server.config.configFile) {
throw new Error(
'Missing configurationFile. The "coverage.thresholds.autoUpdate" can only be enabled when configuration file is used.',
)
}
const configFilePath = this.ctx.server.config.configFile
const configModule = await this.parseConfigModule(configFilePath)
await this.updateThresholds({
thresholds: resolvedThresholds,
configurationFile: configModule,
onUpdate: () =>
writeFileSync(
configFilePath,
configModule.generate().code,
'utf-8',
),
})
}
}
/**
* Constructs collected coverage and users' threshold options into separate sets
* where each threshold set holds their own coverage maps. Threshold set is either
* for specific files defined by glob pattern or global for all other files.
*/
private resolveThresholds(coverageMap: CoverageMap): ResolvedThreshold[] {
const resolvedThresholds: ResolvedThreshold[] = []
const files = coverageMap.files()
const globalCoverageMap = this.createCoverageMap()
for (const key of Object.keys(this.options.thresholds!) as `${keyof NonNullable<typeof this.options.thresholds>}`[]) {
if (
key === 'perFile'
|| key === 'autoUpdate'
|| key === '100'
|| THRESHOLD_KEYS.includes(key)
) {
continue
}
const glob = key
const globThresholds = resolveGlobThresholds(this.options.thresholds![glob])
const globCoverageMap = this.createCoverageMap()
const matchingFiles = files.filter(file =>
mm.isMatch(relative(this.ctx.config.root, file), glob),
)
for (const file of matchingFiles) {
const fileCoverage = coverageMap.fileCoverageFor(file)
globCoverageMap.addFileCoverage(fileCoverage)
}
resolvedThresholds.push({
name: glob,
coverageMap: globCoverageMap,
thresholds: globThresholds,
})
}
// Global threshold is for all files, even if they are included by glob patterns
for (const file of files) {
const fileCoverage = coverageMap.fileCoverageFor(file)
globalCoverageMap.addFileCoverage(fileCoverage)
}
resolvedThresholds.unshift({
name: GLOBAL_THRESHOLDS_KEY,
coverageMap: globalCoverageMap,
thresholds: {
branches: this.options.thresholds?.branches,
functions: this.options.thresholds?.functions,
lines: this.options.thresholds?.lines,
statements: this.options.thresholds?.statements,
},
})
return resolvedThresholds
}
/**
* Check collected coverage against configured thresholds. Sets exit code to 1 when thresholds not reached.
*/
private checkThresholds(allThresholds: ResolvedThreshold[]) {
for (const { coverageMap, thresholds, name } of allThresholds) {
if (
thresholds.branches === undefined
&& thresholds.functions === undefined
&& thresholds.lines === undefined
&& thresholds.statements === undefined
) {
continue
}
// Construct list of coverage summaries where thresholds are compared against
const summaries = this.options.thresholds?.perFile
? coverageMap.files().map((file: string) => ({
file,
summary: coverageMap.fileCoverageFor(file).toSummary(),
}))
: [{ file: null, summary: coverageMap.getCoverageSummary() }]
// Check thresholds of each summary
for (const { summary, file } of summaries) {
for (const thresholdKey of THRESHOLD_KEYS) {
const threshold = thresholds[thresholdKey]
if (threshold !== undefined) {
const coverage = summary.data[thresholdKey].pct
if (coverage < threshold) {
process.exitCode = 1
/*
* Generate error message based on perFile flag:
* - ERROR: Coverage for statements (33.33%) does not meet threshold (85%) for src/math.ts
* - ERROR: Coverage for statements (50%) does not meet global threshold (85%)
*/
let errorMessage = `ERROR: Coverage for ${thresholdKey} (${coverage}%) does not meet ${
name === GLOBAL_THRESHOLDS_KEY ? name : `"${name}"`
} threshold (${threshold}%)`
if (this.options.thresholds?.perFile && file) {
errorMessage += ` for ${relative('./', file).replace(/\\/g, '/')}`
}
this.ctx.logger.error(errorMessage)
}
}
}
}
}
}
/**
* Check if current coverage is above configured thresholds and bump the thresholds if needed
*/
async updateThresholds({ thresholds: allThresholds, onUpdate, configurationFile }: {
thresholds: ResolvedThreshold[]
configurationFile: unknown // ProxifiedModule from magicast
onUpdate: () => void
}) {
let updatedThresholds = false
const config = resolveConfig(configurationFile)
assertConfigurationModule(config)
for (const { coverageMap, thresholds, name } of allThresholds) {
const summaries = this.options.thresholds?.perFile
? coverageMap
.files()
.map((file: string) =>
coverageMap.fileCoverageFor(file).toSummary(),
)
: [coverageMap.getCoverageSummary()]
const thresholdsToUpdate: [Threshold, number][] = []
for (const key of THRESHOLD_KEYS) {
const threshold = thresholds[key] ?? 100
const actual = Math.min(
...summaries.map(summary => summary[key].pct),
)
if (actual > threshold) {
thresholdsToUpdate.push([key, actual])
}
}
if (thresholdsToUpdate.length === 0) {
continue
}
updatedThresholds = true
for (const [threshold, newValue] of thresholdsToUpdate) {
if (name === GLOBAL_THRESHOLDS_KEY) {
config.test.coverage.thresholds[threshold] = newValue
}
else {
const glob = config.test.coverage.thresholds[name as Threshold] as ResolvedThreshold['thresholds']
glob[threshold] = newValue
}
}
}
if (updatedThresholds) {
this.ctx.logger.log('Updating thresholds to configuration file. You may want to push with updated coverage thresholds.')
onUpdate()
}
}
async mergeReports(coverageMaps: unknown[]): Promise<void> {
const coverageMap = this.createCoverageMap()
for (const coverage of coverageMaps) {
coverageMap.merge(coverage as CoverageMap)
}
await this.generateReports(coverageMap, true)
}
hasTerminalReporter(reporters: ResolvedCoverageOptions['reporter']) {
return reporters.some(
([reporter]) =>
reporter === 'text'
|| reporter === 'text-summary'
|| reporter === 'text-lcov'
|| reporter === 'teamcity',
)
}
toSlices<T>(array: T[], size: number): T[][] {
return array.reduce<T[][]>((chunks, item) => {
const index = Math.max(0, chunks.length - 1)
const lastChunk = chunks[index] || []
chunks[index] = lastChunk
if (lastChunk.length >= size) {
chunks.push([item])
}
else {
lastChunk.push(item)
}
return chunks
}, [])
}
createUncoveredFileTransformer(ctx: Vitest) {
const servers = [
...ctx.projects.map(project => ({
root: project.config.root,
vitenode: project.vitenode,
})),
// Check core last as it will match all files anyway
{ root: ctx.config.root, vitenode: ctx.vitenode },
]
return async function transformFile(filename: string) {
let lastError
for (const { root, vitenode } of servers) {
if (!filename.startsWith(root)) {
continue
}
try {
return await vitenode.transformRequest(filename)
}
catch (error) {
lastError = error
}
}
// All vite-node servers failed to transform the file
throw lastError
}
}
}
/**
* Narrow down `unknown` glob thresholds to resolved ones
*/
function resolveGlobThresholds(
thresholds: unknown,
): ResolvedThreshold['thresholds'] {
if (!thresholds || typeof thresholds !== 'object') {
return {}
}
if (100 in thresholds && thresholds[100] === true) {
return {
lines: 100,
branches: 100,
functions: 100,
statements: 100,
}
}
return {
lines:
'lines' in thresholds && typeof thresholds.lines === 'number'
? thresholds.lines
: undefined,
branches:
'branches' in thresholds && typeof thresholds.branches === 'number'
? thresholds.branches
: undefined,
functions:
'functions' in thresholds && typeof thresholds.functions === 'number'
? thresholds.functions
: undefined,
statements:
'statements' in thresholds && typeof thresholds.statements === 'number'
? thresholds.statements
: undefined,
}
}
function assertConfigurationModule(config: unknown): asserts config is {
test: {
coverage: { thresholds: NonNullable<BaseCoverageOptions['thresholds']> }
}
} {
try {
// @ts-expect-error -- Intentional unsafe null pointer check as wrapped in try-catch
if (typeof config.test.coverage.thresholds !== 'object') {
throw new TypeError(
'Expected config.test.coverage.thresholds to be an object',
)
}
}
catch (error) {
const message = error instanceof Error ? error.message : String(error)
throw new Error(
`Unable to parse thresholds from configuration file: ${message}`,
)
}
}
function resolveConfig(configModule: any) {
const mod = configModule.exports.default
try {
// Check for "export default { test: {...} }"
if (mod.$type === 'object') {
return mod
}
// "export default defineConfig(...)"
let config = resolveDefineConfig(mod)
if (config) {
return config
}
// "export default mergeConfig(..., defineConfig(...))"
if (mod.$type === 'function-call' && mod.$callee === 'mergeConfig') {
config = resolveMergeConfig(mod)
if (config) {
return config
}
}
}
catch (error) {
// Reduce magicast's verbose errors to readable ones
throw new Error(error instanceof Error ? error.message : String(error))
}
throw new Error(
'Failed to update coverage thresholds. Configuration file is too complex.',
)
}
function resolveDefineConfig(mod: any) {
if (mod.$type === 'function-call' && mod.$callee === 'defineConfig') {
// "export default defineConfig({ test: {...} })"
if (mod.$args[0].$type === 'object') {
return mod.$args[0]
}
if (mod.$args[0].$type === 'arrow-function-expression') {
if (mod.$args[0].$body.$type === 'object') {
// "export default defineConfig(() => ({ test: {...} }))"
return mod.$args[0].$body
}
// "export default defineConfig(() => mergeConfig({...}, ...))"
const config = resolveMergeConfig(mod.$args[0].$body)
if (config) {
return config
}
}
}
}
function resolveMergeConfig(mod: any): any {
if (mod.$type === 'function-call' && mod.$callee === 'mergeConfig') {
for (const arg of mod.$args) {
const config = resolveDefineConfig(arg)
if (config) {
return config
}
}
}
}