Skip to content

Commit

Permalink
fix(coverage): thresholds to compare files relative to root (#5574)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Apr 22, 2024
1 parent 316eb73 commit 80265b4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
coverageMap,
thresholds: this.options.thresholds,
createCoverageMap: () => libCoverage.createCoverageMap({}),
root: this.ctx.config.root,
})

this.checkThresholds({
Expand Down
1 change: 1 addition & 0 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
coverageMap,
thresholds: this.options.thresholds,
createCoverageMap: () => libCoverage.createCoverageMap({}),
root: this.ctx.config.root,
})

this.checkThresholds({
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ export class BaseCoverageProvider {
* where each threshold set holds their own coverage maps. Threshold set is either
* for specific files defined by glob pattern or global for all other files.
*/
resolveThresholds({ coverageMap, thresholds, createCoverageMap }: {
resolveThresholds({ coverageMap, thresholds, createCoverageMap, root }: {
coverageMap: CoverageMap
thresholds: NonNullable<BaseCoverageOptions['thresholds']>
createCoverageMap: () => CoverageMap
root: string
}): ResolvedThreshold[] {
const resolvedThresholds: ResolvedThreshold[] = []
const files = coverageMap.files()
Expand All @@ -143,7 +144,7 @@ export class BaseCoverageProvider {
const globThresholds = resolveGlobThresholds(thresholds[glob])
const globCoverageMap = createCoverageMap()

const matchingFiles = files.filter(file => mm.isMatch(file, glob))
const matchingFiles = files.filter(file => mm.isMatch(relative(root, file), glob))
filesMatchedByGlobs.push(...matchingFiles)

for (const file of matchingFiles) {
Expand Down
6 changes: 6 additions & 0 deletions test/coverage-test/option-tests/thresholds.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from 'vitest'
import { add } from '../src/utils'

test('cover some lines, but not too much', () => {
add(1, 2)
})
29 changes: 28 additions & 1 deletion test/coverage-test/testing-options.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ const testCases = [
include: ['coverage-report-tests/empty-lines.test.ts'],
},
},
{
testConfig: {
name: 'failing thresholds',
include: ['option-tests/thresholds.test.ts'],
coverage: {
reporter: 'text',
all: false,
include: ['src/utils.ts'],
thresholds: {
'src/utils.ts': {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
},
},
after() {
if (process.exitCode !== 1)
throw new Error('Expected test to fail as thresholds are not met')

process.exitCode = 0
},
},
]

for (const provider of ['v8', 'istanbul']) {
Expand Down Expand Up @@ -149,6 +174,8 @@ for (const provider of ['v8', 'istanbul']) {
}

function checkExit() {
if (process.exitCode)
if (process.exitCode) {
console.error(`Exit code was set to ${process.exitCode}. Failing tests`)
process.exit(process.exitCode)
}
}

0 comments on commit 80265b4

Please sign in to comment.