Skip to content

Commit

Permalink
fix: don't panic when coverage.reporter is a string (#6267)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 2, 2024
1 parent 081cfe0 commit 7b37d27
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
25 changes: 25 additions & 0 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { RandomSequencer } from '../sequencers/RandomSequencer'
import type { BenchmarkBuiltinReporters } from '../reporters'
import { builtinPools } from '../pool'
import type { Logger } from '../logger'
import type { BaseCoverageOptions, CoverageReporterWithOptions } from '../types/coverage'

function resolvePath(path: string, root: string) {
return normalize(
Expand Down Expand Up @@ -229,6 +230,8 @@ export function resolveConfig(
)
}

resolved.coverage.reporter = resolveCoverageReporters(resolved.coverage.reporter)

if (resolved.coverage.enabled && resolved.coverage.reportsDirectory) {
const reportsDirectory = resolve(
resolved.root,
Expand Down Expand Up @@ -789,3 +792,25 @@ export function resolveConfig(
export function isBrowserEnabled(config: ResolvedConfig): boolean {
return Boolean(config.browser?.enabled)
}

export function resolveCoverageReporters(configReporters: NonNullable<BaseCoverageOptions['reporter']>): CoverageReporterWithOptions[] {
// E.g. { reporter: "html" }
if (!Array.isArray(configReporters)) {
return [[configReporters, {}]]
}

const resolvedReporters: CoverageReporterWithOptions[] = []

for (const reporter of configReporters) {
if (Array.isArray(reporter)) {
// E.g. { reporter: [ ["html", { skipEmpty: true }], ["lcov"], ["json", { file: "map.json" }] ]}
resolvedReporters.push([reporter[0], reporter[1] as Record<string, unknown> || {}])
}
else {
// E.g. { reporter: ["html", "json"]}
resolvedReporters.push([reporter, {}])
}
}

return resolvedReporters
}
2 changes: 1 addition & 1 deletion packages/vitest/src/node/types/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface CoverageProviderModule {

export type CoverageReporter = keyof ReportOptions | (string & {})

type CoverageReporterWithOptions<
export type CoverageReporterWithOptions<
ReporterName extends CoverageReporter = CoverageReporter,
> = ReporterName extends keyof ReportOptions
? ReportOptions[ReporterName] extends never
Expand Down
21 changes: 2 additions & 19 deletions packages/vitest/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { relative } from 'pathe'
import mm from 'micromatch'
import type { CoverageMap } from 'istanbul-lib-coverage'
import type { BaseCoverageOptions, ResolvedCoverageOptions } from '../node/types/coverage'
import { resolveCoverageReporters } from '../node/config/resolveConfig'

type Threshold = 'lines' | 'functions' | 'statements' | 'branches'

Expand Down Expand Up @@ -242,25 +243,7 @@ export class BaseCoverageProvider {
resolveReporters(
configReporters: NonNullable<BaseCoverageOptions['reporter']>,
): ResolvedCoverageOptions['reporter'] {
// E.g. { reporter: "html" }
if (!Array.isArray(configReporters)) {
return [[configReporters, {}]]
}

const resolvedReporters: ResolvedCoverageOptions['reporter'] = []

for (const reporter of configReporters) {
if (Array.isArray(reporter)) {
// E.g. { reporter: [ ["html", { skipEmpty: true }], ["lcov"], ["json", { file: "map.json" }] ]}
resolvedReporters.push([reporter[0], reporter[1] as Record<string, unknown> || {}])
}
else {
// E.g. { reporter: ["html", "json"]}
resolvedReporters.push([reporter, {}])
}
}

return resolvedReporters
return resolveCoverageReporters(configReporters) as any
}

hasTerminalReporter(reporters: ResolvedCoverageOptions['reporter']) {
Expand Down
20 changes: 19 additions & 1 deletion test/coverage-test/test/reporters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readdirSync } from 'node:fs'
import { existsSync, readdirSync } from 'node:fs'
import { rm } from 'node:fs/promises'
import { expect } from 'vitest'
import { runVitest, test } from '../utils'

Expand All @@ -17,6 +18,23 @@ test('reporter as string', async () => {
expect(files).toContain('coverage-final.json')
})

test('reporter as string when coverage is disabled', async () => {
if (existsSync('./coverage')) {
await rm('./coverage', { recursive: true, force: true })
}

await runVitest({
include,
coverage: {
enabled: false,
reporter: 'json',
all: false,
},
})

expect(existsSync('./coverage')).toBe(false)
})

test('reporter as list of strings', async () => {
await runVitest({
include,
Expand Down

0 comments on commit 7b37d27

Please sign in to comment.