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 Aug 2, 2022
1 parent 112e9c5 commit a8d1d61
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 { BaseCoverageProvider } from './base'

export class NullCoverageProvider implements BaseCoverageProvider {
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,7 +1,7 @@
import type { CoverageOptions } from '../../types'

import type { BaseCoverageProvider } from './base'
import { C8CoverageProvider } from './c8'
import { NullCoverageProvider } from './NullCoverageProvider'

const CoverageProviderMap: Record<
NonNullable<CoverageOptions['provider']>,
Expand All @@ -17,8 +17,7 @@ export function getCoverageProvider(options?: CoverageOptions): BaseCoverageProv
return new CoverageProvider()
}

// TODO: Return NullCoverageProvider
return new C8CoverageProvider()
return new NullCoverageProvider()
}

export function getCoverageInsideWorker(options: CoverageOptions) {
Expand Down
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.coverageProvider.clean(this.config.coverage.clean)
await this.coverageProvider.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.coverageProvider.onAfterAllFilesRun()
await this.coverageProvider.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.coverageProvider.clean()

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

await this.runFiles(files)

if (this.config.coverage.enabled)
await this.coverageProvider.onAfterAllFilesRun()
await this.coverageProvider.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.coverageProvider.onBeforeFilesRun?.()
ctx.coverageProvider.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 a8d1d61

Please sign in to comment.