Skip to content

Commit e30dd9c

Browse files
innoprejinnopreAriPerkkio
authored
fix(coverage): avoid matching sibling project roots (#10311)
Co-authored-by: Shin JaeHee <jaeheesin@gmail.com> Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>
1 parent 65547d6 commit e30dd9c

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

packages/vitest/src/node/coverage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ export class BaseCoverageProvider {
156156
return cacheHit
157157
}
158158

159+
const matchingRoot = roots.find(root => filename.startsWith(`${slash(root)}/`) || filename === slash(root))
160+
159161
// File outside project root with default allowExternal
160-
if (this.options.allowExternal === false && roots.every(root => !filename.startsWith(root))) {
162+
if (this.options.allowExternal === false && !matchingRoot) {
161163
this.globCache.set(filename, false)
162164

163165
return false
164166
}
165167

166-
const matchingRoot = roots.find(root => filename.startsWith(`${slash(root)}/`) || filename === slash(root))
167168
const relativeFilename = matchingRoot ? relative(matchingRoot, filename) : filename
168169

169170
if (pm.isMatch(relativeFilename, this.options.exclude, { dot: true })) {

test/coverage-test/test/include-exclude.unit.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,23 @@ test('files outside project when allowExternal: true', async () => {
144144
expect(isIncluded(resolve(process.cwd(), '../../package-b/src/two.ts'))).toBe(false)
145145
})
146146

147-
async function init(options: Partial<CoverageOptions> & { testInclude?: string[] }) {
147+
test('files with almost matching name, outside project when allowExternal: false', async () => {
148+
const isIncluded = await init({
149+
include: ['**/*.ts'],
150+
root: './something/',
151+
allowExternal: false,
152+
})
153+
154+
expect(isIncluded(resolve(process.cwd(), './something/src/one.ts'))).toBe(true)
155+
expect(isIncluded(resolve(process.cwd(), './not-something/src/two.ts'))).toBe(false)
156+
expect(isIncluded(resolve(process.cwd(), './something-else/src/three.ts'))).toBe(false)
157+
})
158+
159+
async function init(options: Partial<CoverageOptions> & { testInclude?: string[]; root?: string }) {
148160
const vitest = await createVitest('test', {
149161
config: false,
150162
include: ['dont-match-anything', ...(options.testInclude || [])],
163+
root: options.root,
151164
coverage: {
152165
...options,
153166
enabled: true,

0 commit comments

Comments
 (0)