Skip to content

Commit 80265b4

Browse files
authoredApr 22, 2024··
fix(coverage): thresholds to compare files relative to root (#5574)
1 parent 316eb73 commit 80265b4

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed
 

‎packages/coverage-istanbul/src/provider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
225225
coverageMap,
226226
thresholds: this.options.thresholds,
227227
createCoverageMap: () => libCoverage.createCoverageMap({}),
228+
root: this.ctx.config.root,
228229
})
229230

230231
this.checkThresholds({

‎packages/coverage-v8/src/provider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
216216
coverageMap,
217217
thresholds: this.options.thresholds,
218218
createCoverageMap: () => libCoverage.createCoverageMap({}),
219+
root: this.ctx.config.root,
219220
})
220221

221222
this.checkThresholds({

‎packages/vitest/src/utils/coverage.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ export class BaseCoverageProvider {
125125
* where each threshold set holds their own coverage maps. Threshold set is either
126126
* for specific files defined by glob pattern or global for all other files.
127127
*/
128-
resolveThresholds({ coverageMap, thresholds, createCoverageMap }: {
128+
resolveThresholds({ coverageMap, thresholds, createCoverageMap, root }: {
129129
coverageMap: CoverageMap
130130
thresholds: NonNullable<BaseCoverageOptions['thresholds']>
131131
createCoverageMap: () => CoverageMap
132+
root: string
132133
}): ResolvedThreshold[] {
133134
const resolvedThresholds: ResolvedThreshold[] = []
134135
const files = coverageMap.files()
@@ -143,7 +144,7 @@ export class BaseCoverageProvider {
143144
const globThresholds = resolveGlobThresholds(thresholds[glob])
144145
const globCoverageMap = createCoverageMap()
145146

146-
const matchingFiles = files.filter(file => mm.isMatch(file, glob))
147+
const matchingFiles = files.filter(file => mm.isMatch(relative(root, file), glob))
147148
filesMatchedByGlobs.push(...matchingFiles)
148149

149150
for (const file of matchingFiles) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from 'vitest'
2+
import { add } from '../src/utils'
3+
4+
test('cover some lines, but not too much', () => {
5+
add(1, 2)
6+
})

‎test/coverage-test/testing-options.mjs

+28-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,31 @@ const testCases = [
106106
include: ['coverage-report-tests/empty-lines.test.ts'],
107107
},
108108
},
109+
{
110+
testConfig: {
111+
name: 'failing thresholds',
112+
include: ['option-tests/thresholds.test.ts'],
113+
coverage: {
114+
reporter: 'text',
115+
all: false,
116+
include: ['src/utils.ts'],
117+
thresholds: {
118+
'src/utils.ts': {
119+
branches: 100,
120+
functions: 100,
121+
lines: 100,
122+
statements: 100,
123+
},
124+
},
125+
},
126+
},
127+
after() {
128+
if (process.exitCode !== 1)
129+
throw new Error('Expected test to fail as thresholds are not met')
130+
131+
process.exitCode = 0
132+
},
133+
},
109134
]
110135

111136
for (const provider of ['v8', 'istanbul']) {
@@ -149,6 +174,8 @@ for (const provider of ['v8', 'istanbul']) {
149174
}
150175

151176
function checkExit() {
152-
if (process.exitCode)
177+
if (process.exitCode) {
178+
console.error(`Exit code was set to ${process.exitCode}. Failing tests`)
153179
process.exit(process.exitCode)
180+
}
154181
}

0 commit comments

Comments
 (0)
Please sign in to comment.