Skip to content

Commit

Permalink
feat(coverage): glob based coverage thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 12, 2023
1 parent cc19117 commit 01036c9
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 174 deletions.
1 change: 1 addition & 0 deletions packages/coverage-istanbul/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"istanbul-lib-report": "^3.0.1",
"istanbul-lib-source-maps": "^4.0.1",
"istanbul-reports": "^3.1.6",
"magicast": "^0.3.2",
"picocolors": "^1.0.0",
"test-exclude": "^6.0.0"
},
Expand Down
45 changes: 17 additions & 28 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { AfterSuiteRunMeta, CoverageIstanbulOptions, CoverageProvider, Repo
import { coverageConfigDefaults, defaultExclude, defaultInclude } from 'vitest/config'
import { BaseCoverageProvider } from 'vitest/coverage'
import c from 'picocolors'
import { parseModule } from 'magicast'
import libReport from 'istanbul-lib-report'
import reports from 'istanbul-reports'
import type { CoverageMap, CoverageMapData } from 'istanbul-lib-coverage'
Expand Down Expand Up @@ -65,10 +66,6 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
provider: 'istanbul',
reportsDirectory: resolve(ctx.config.root, config.reportsDirectory || coverageConfigDefaults.reportsDirectory),
reporter: this.resolveReporters(config.reporter || coverageConfigDefaults.reporter),
lines: config['100'] ? 100 : config.lines,
functions: config['100'] ? 100 : config.functions,
branches: config['100'] ? 100 : config.branches,
statements: config['100'] ? 100 : config.statements,
}

this.instrumenter = createInstrumenter({
Expand Down Expand Up @@ -170,34 +167,26 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
}).execute(context)
}

if (this.options.branches
|| this.options.functions
|| this.options.lines
|| this.options.statements) {
this.checkThresholds({
if (this.options.thresholds) {
const resolvedThresholds = this.resolveThresholds({
coverageMap,
thresholds: {
branches: this.options.branches,
functions: this.options.functions,
lines: this.options.lines,
statements: this.options.statements,
},
perFile: this.options.perFile,
thresholds: this.options.thresholds,
createCoverageMap: () => libCoverage.createCoverageMap({}),
})
}

if (this.options.thresholdAutoUpdate && allTestsRun) {
this.updateThresholds({
coverageMap,
thresholds: {
branches: this.options.branches,
functions: this.options.functions,
lines: this.options.lines,
statements: this.options.statements,
},
perFile: this.options.perFile,
configurationFile: this.ctx.server.config.configFile,
this.checkThresholds({
thresholds: resolvedThresholds,
perFile: this.options.thresholds.perFile,
})

if (this.options.thresholds.autoUpdate && allTestsRun) {
this.updateThresholds({
thresholds: resolvedThresholds,
perFile: this.options.thresholds.perFile,
configurationFile: this.ctx.server.config.configFile,
parseModule,
})
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/coverage-v8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"istanbul-lib-source-maps": "^4.0.1",
"istanbul-reports": "^3.1.6",
"magic-string": "^0.30.5",
"magicast": "^0.3.2",
"picocolors": "^1.0.0",
"std-env": "^3.4.3",
"test-exclude": "^6.0.0",
Expand Down
45 changes: 17 additions & 28 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { CoverageMap, CoverageMapData } from 'istanbul-lib-coverage'
import libCoverage from 'istanbul-lib-coverage'
import libSourceMaps from 'istanbul-lib-source-maps'
import MagicString from 'magic-string'
import { parseModule } from 'magicast'
import remapping from '@ampproject/remapping'
import { normalize, resolve } from 'pathe'
import c from 'picocolors'
Expand Down Expand Up @@ -72,10 +73,6 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
provider: 'v8',
reporter: this.resolveReporters(config.reporter || coverageConfigDefaults.reporter),
reportsDirectory: resolve(ctx.config.root, config.reportsDirectory || coverageConfigDefaults.reportsDirectory),
lines: config['100'] ? 100 : config.lines,
functions: config['100'] ? 100 : config.functions,
branches: config['100'] ? 100 : config.branches,
statements: config['100'] ? 100 : config.statements,
}

this.testExclude = new _TestExclude({
Expand Down Expand Up @@ -156,34 +153,26 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
}).execute(context)
}

if (this.options.branches
|| this.options.functions
|| this.options.lines
|| this.options.statements) {
this.checkThresholds({
if (this.options.thresholds) {
const resolvedThresholds = this.resolveThresholds({
coverageMap,
thresholds: {
branches: this.options.branches,
functions: this.options.functions,
lines: this.options.lines,
statements: this.options.statements,
},
perFile: this.options.perFile,
thresholds: this.options.thresholds,
createCoverageMap: () => libCoverage.createCoverageMap({}),
})
}

if (this.options.thresholdAutoUpdate && allTestsRun) {
this.updateThresholds({
coverageMap,
thresholds: {
branches: this.options.branches,
functions: this.options.functions,
lines: this.options.lines,
statements: this.options.statements,
},
perFile: this.options.perFile,
configurationFile: this.ctx.server.config.configFile,
this.checkThresholds({
thresholds: resolvedThresholds,
perFile: this.options.thresholds.perFile,
})

if (this.options.thresholds.autoUpdate && allTestsRun) {
this.updateThresholds({
thresholds: resolvedThresholds,
perFile: this.options.thresholds.perFile,
configurationFile: this.ctx.server.config.configFile,
parseModule,
})
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
"happy-dom": "^12.10.3",
"jsdom": "^22.1.0",
"log-update": "^6.0.0",
"magicast": "^0.3.2",
"micromatch": "^4.0.5",
"p-limit": "^5.0.0",
"pretty-format": "^29.7.0",
Expand Down
88 changes: 44 additions & 44 deletions packages/vitest/src/types/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,40 +157,27 @@ export interface BaseCoverageOptions {
skipFull?: boolean

/**
* Check thresholds per file.
* See `lines`, `functions`, `branches` and `statements` for the actual thresholds.
* Configurations for thresholds
*
* @default false
*/
perFile?: boolean

/**
* Threshold for lines
*
* @default undefined
*/
lines?: number

/**
* Threshold for functions
*
* @default undefined
*/
functions?: number

/**
* Threshold for branches
* @example
*
* @default undefined
*/
branches?: number

/**
* Threshold for statements
* ```ts
* {
* // Thresholds for all files
* functions: 95,
* branches: 70,
* perFile: true,
* autoUpdate: true,
*
* @default undefined
* // Thresholds for utilities
* 'src/utils/**.ts': {
* lines: 100,
* statements: 95,
* }
* }
* ```
*/
statements?: number
thresholds?: Thresholds | ({ [glob: string]: Pick<Thresholds, 'statements' | 'functions' | 'branches' | 'lines'> } & Thresholds)

/**
* Watermarks for statements, lines, branches and functions.
Expand All @@ -204,13 +191,6 @@ export interface BaseCoverageOptions {
lines?: [number, number]
}

/**
* Update threshold values automatically when current coverage is higher than earlier thresholds
*
* @default false
*/
thresholdAutoUpdate?: boolean

/**
* Generate coverage report even when tests fail.
*
Expand All @@ -224,13 +204,6 @@ export interface BaseCoverageOptions {
* @default false
*/
allowExternal?: boolean

/**
* Shortcut for `{ lines: 100, functions: 100, branches: 100, statements: 100 }`
*
* @default false
*/
100?: boolean
}

export interface CoverageIstanbulOptions extends BaseCoverageOptions {
Expand All @@ -248,3 +221,30 @@ export interface CustomProviderOptions extends Pick<BaseCoverageOptions, FieldsW
/** Name of the module or path to a file to load the custom provider from */
customProviderModule: string
}

interface Thresholds {
/** Set global thresholds to `100` */
100?: boolean

/** Check thresholds per file. */
perFile?: boolean

/**
* Update threshold values automatically when current coverage is higher than earlier thresholds
*
* @default false
*/
autoUpdate?: boolean

/** Thresholds for statements */
statements?: number

/** Thresholds for functions */
functions?: number

/** Thresholds for branches */
branches?: number

/** Thresholds for lines */
lines?: number
}
Loading

0 comments on commit 01036c9

Please sign in to comment.