Skip to content

Commit

Permalink
fix(coverage): watch mode to use coverage.all only when all tests a…
Browse files Browse the repository at this point in the history
…re run (#2665)

* fix(coverage): always print reporter name

* fix(coverage): report on watch mode manually triggered re-runs

* fix(coverage): watch mode to use `coverage.all` only when all tests are run
  • Loading branch information
AriPerkkio authored Jan 16, 2023
1 parent 2d90270 commit 8509628
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
14 changes: 10 additions & 4 deletions packages/coverage-c8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { extname, resolve } from 'pathe'
import type { RawSourceMap } from 'vite-node'
import { configDefaults } from 'vitest/config'
// eslint-disable-next-line no-restricted-imports
import type { CoverageC8Options, CoverageProvider, ResolvedCoverageOptions } from 'vitest'
import type { CoverageC8Options, CoverageProvider, ReportContext, ResolvedCoverageOptions } from 'vitest'
import type { Vitest } from 'vitest/node'
// @ts-expect-error missing types
import createReport from 'c8/lib/report.js'
Expand Down Expand Up @@ -44,9 +44,15 @@ export class C8CoverageProvider implements CoverageProvider {
takeCoverage()
}

async reportCoverage() {
async reportCoverage({ allTestsRun }: ReportContext) {
takeCoverage()
const report = createReport(this.ctx.config.coverage)

const options = {
...this.options,
all: this.options.all && allTestsRun,
}

const report = createReport(options)

interface MapAndSource { map: RawSourceMap; source: string | undefined }
type SourceMapMeta = { url: string; filepath: string } & MapAndSource
Expand Down Expand Up @@ -136,7 +142,7 @@ export class C8CoverageProvider implements CoverageProvider {
}

await report.run()
await checkCoverages(this.options, report)
await checkCoverages(options, report)

// Note that this will only clean up the V8 reports generated so far.
// There will still be a temp directory with some reports when vitest exists,
Expand Down
6 changes: 3 additions & 3 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { existsSync, promises as fs } from 'fs'
import { relative, resolve } from 'pathe'
import type { TransformPluginContext } from 'rollup'
import type { AfterSuiteRunMeta, CoverageIstanbulOptions, CoverageProvider, ResolvedCoverageOptions, Vitest } from 'vitest'
import type { AfterSuiteRunMeta, CoverageIstanbulOptions, CoverageProvider, ReportContext, ResolvedCoverageOptions, Vitest } from 'vitest'
import { configDefaults, defaultExclude, defaultInclude } from 'vitest/config'
import libReport from 'istanbul-lib-report'
import reports from 'istanbul-reports'
Expand Down Expand Up @@ -98,14 +98,14 @@ export class IstanbulCoverageProvider implements CoverageProvider {
this.coverages = []
}

async reportCoverage() {
async reportCoverage({ allTestsRun }: ReportContext) {
const mergedCoverage: CoverageMap = this.coverages.reduce((coverage, previousCoverageMap) => {
const map = libCoverage.createCoverageMap(coverage)
map.merge(previousCoverageMap)
return map
}, {})

if (this.options.all)
if (this.options.all && allTestsRun)
await this.includeUntestedFiles(mergedCoverage)

includeImplicitElseBranches(mergedCoverage)
Expand Down
20 changes: 15 additions & 5 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,7 @@ export class Vitest {

await this.runFiles(files)

if (this.coverageProvider) {
this.logger.log(c.blue(' % ') + c.dim('Coverage report from ') + c.yellow(this.coverageProvider.name))
await this.coverageProvider.reportCoverage()
}
await this.reportCoverage(true)

if (this.config.watch && !this.config.browser)
await this.report('onWatcherStart')
Expand Down Expand Up @@ -357,8 +354,14 @@ export class Vitest {
}

async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string) {
if (this.coverageProvider && this.config.coverage.cleanOnRerun)
await this.coverageProvider.clean()

await this.report('onWatcherRerun', files, trigger)
await this.runFiles(files)

await this.reportCoverage(!trigger)

if (!this.config.browser)
await this.report('onWatcherStart')
}
Expand Down Expand Up @@ -435,7 +438,7 @@ export class Vitest {

await this.runFiles(files)

await this.coverageProvider?.reportCoverage()
await this.reportCoverage(false)

if (!this.config.browser)
await this.report('onWatcherStart')
Expand Down Expand Up @@ -533,6 +536,13 @@ export class Vitest {
return rerun
}

private async reportCoverage(allTestsRun: boolean) {
if (this.coverageProvider) {
this.logger.log(c.blue(' % ') + c.dim('Coverage report from ') + c.yellow(this.coverageProvider.name))
await this.coverageProvider.reportCoverage({ allTestsRun })
}
}

async close() {
if (!this.closingPromise) {
this.closingPromise = Promise.allSettled([
Expand Down
7 changes: 6 additions & 1 deletion packages/vitest/src/types/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface CoverageProvider {
onBeforeFilesRun?(): void | Promise<void>
onAfterSuiteRun(meta: AfterSuiteRunMeta): void | Promise<void>

reportCoverage(): void | Promise<void>
reportCoverage(reportContext: ReportContext): void | Promise<void>

onFileTransform?(
sourceCode: string,
Expand All @@ -22,6 +22,11 @@ export interface CoverageProvider {
): TransformResult | Promise<TransformResult>
}

export interface ReportContext {
/** Indicates whether all tests were run. False when only specific tests were run. */
allTestsRun?: boolean
}

export interface CoverageProviderModule {
/**
* Factory for creating a new coverage provider
Expand Down

0 comments on commit 8509628

Please sign in to comment.