Skip to content

Commit

Permalink
chore: resolve coverage reporter using coverage resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 2, 2024
1 parent 137c5d8 commit b420512
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
25 changes: 24 additions & 1 deletion 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,7 +230,7 @@ export function resolveConfig(
)
}

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

if (resolved.coverage.enabled && resolved.coverage.reportsDirectory) {
const reportsDirectory = resolve(
Expand Down Expand Up @@ -791,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

0 comments on commit b420512

Please sign in to comment.