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 11, 2023
1 parent cc19117 commit 23dee73
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 176 deletions.
43 changes: 15 additions & 28 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,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 +166,25 @@ 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,
})
}
}
}

Expand Down
43 changes: 15 additions & 28 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,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 +152,25 @@ 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,
})
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"execa": "^8.0.1",
"local-pkg": "^0.5.0",
"magic-string": "^0.30.5",
"magicast": "^0.3.2",
"pathe": "^1.1.1",
"picocolors": "^1.0.0",
"std-env": "^3.4.3",
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 23dee73

Please sign in to comment.