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 23, 2022
1 parent f16a9e3 commit 1402584
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 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() {}
}
5 changes: 2 additions & 3 deletions packages/vitest/src/integrations/coverage/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { CoverageOptions } from '../../types'

import type { BaseCoverageReporter } from './base'
import { C8Reporter } from './c8'
import { NullCoverageProvider } from './NullCoverageProvider'

export function getCoverageProvider(options?: CoverageOptions): BaseCoverageReporter {
if (options?.enabled && options?.provider === 'c8')
return new C8Reporter()

// TODO: Return NullCoverageProvider
return new C8Reporter()
return new NullCoverageProvider()
}
11 changes: 4 additions & 7 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,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 +143,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 +325,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 @@ -15,7 +15,9 @@ export type CoverageReporter =
| 'text-summary'
| 'text'

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

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

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

/**
* Clean coverage report on watch rerun
*
Expand All @@ -38,13 +47,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 1402584

Please sign in to comment.