Skip to content

Commit

Permalink
refactor: add NullCoverageProvider to handled disabled coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 22, 2022
1 parent 7067394 commit b699c0c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
20 changes: 20 additions & 0 deletions packages/vitest/src/integrations/coverage/NullCoverageProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ResolvedCoverageOptions } from '../../types'
import type { BaseCoverageReporter } from './base'

export class NullCoverageProvider implements BaseCoverageReporter {
resolveOptions(): ResolvedCoverageOptions {
return {
provider: null,
enabled: false,
clean: false,
cleanOnRerun: false,
reportsDirectory: 'coverage',
tempDirectory: 'coverage/tmp',
}
}

initialize() {}
clean() {}
onAfterAllFilesRun() {}
onAfterSuiteRun() {}
}
18 changes: 10 additions & 8 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SnapshotManager } from '../integrations/snapshot/manager'
import { clearTimeout, deepMerge, hasFailed, noop, setTimeout, slash } from '../utils'
import type { BaseCoverageReporter } from '../integrations/coverage/base'
import { C8Reporter } from '../integrations/coverage/c8'
import { NullCoverageProvider } from '../integrations/coverage/NullCoverageProvider'
import { createPool } from './pool'
import type { WorkerPool } from './pool'
import { createReporters } from './reporters/utils'
Expand Down Expand Up @@ -85,7 +86,11 @@ export class Vitest {

this.reporters = await createReporters(resolved.reporters, this.runner)

this.coverageReporter = new C8Reporter()
if (options.coverage?.enabled && options.coverage?.provider === 'c8')
this.coverageReporter = new C8Reporter()
else
this.coverageReporter = new NullCoverageProvider()

this.coverageReporter.initialize(this)

this.config.coverage = this.coverageReporter.resolveOptions()
Expand All @@ -94,8 +99,7 @@ export class Vitest {

this._onRestartListeners.forEach(fn => fn())

if (this.config.coverage.enabled)
await this.coverageReporter.clean(this.config.coverage.clean)
await this.coverageReporter.clean(this.config.coverage.clean)

this.cache.results.setConfig(resolved.root, resolved.cache)
try {
Expand Down Expand Up @@ -144,8 +148,7 @@ export class Vitest {

await this.runFiles(files)

if (this.config.coverage.enabled)
await this.coverageReporter.onAfterAllFilesRun()
await this.coverageReporter.onAfterAllFilesRun()

if (this.config.watch && !this.config.browser)
await this.report('onWatcherStart')
Expand Down Expand Up @@ -327,15 +330,14 @@ export class Vitest {
const files = Array.from(this.changedTests)
this.changedTests.clear()

if (this.config.coverage.enabled && this.config.coverage.cleanOnRerun)
if (this.config.coverage.cleanOnRerun)
await this.coverageReporter.clean()

await this.report('onWatcherRerun', files, triggerId)

await this.runFiles(files)

if (this.config.coverage.enabled)
await this.coverageReporter.onAfterAllFilesRun()
await this.coverageReporter.onAfterAllFilesRun()

if (!this.config.browser)
await this.report('onWatcherStart')
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export function createPool(ctx: Vitest): WorkerPool {
options.minThreads = 1
}

if (ctx.config.coverage.enabled)
ctx.coverageReporter.onBeforeFilesRun?.()
ctx.coverageReporter.onBeforeFilesRun?.()

options.env = {
TEST: 'true',
Expand Down
21 changes: 14 additions & 7 deletions packages/vitest/src/types/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export type CoverageReporter =

export type CoverageProviders = 'c8'

export type CoverageOptions = C8Options & { provider?: 'c8' }
export type CoverageOptions =
| NullCoverageOptions & { provider?: null }
| C8Options & { provider?: 'c8' }

interface BaseCoverageOptions {
/**
Expand All @@ -27,6 +29,13 @@ interface BaseCoverageOptions {
*/
enabled?: boolean

/**
* Clean coverage before running tests
*
* @default true
*/
clean?: boolean

/**
* Clean coverage report on watch rerun
*
Expand All @@ -40,13 +49,11 @@ interface BaseCoverageOptions {
reportsDirectory?: string
}

export interface NullCoverageOptions extends BaseCoverageOptions {
enabled: false
}

export interface C8Options extends BaseCoverageOptions {
/**
* Clean coverage before running tests
*
* @default true
*/
clean?: boolean
/**
* Allow files from outside of your cwd.
*
Expand Down

0 comments on commit b699c0c

Please sign in to comment.