Skip to content

fix: extraneous coverage for --project (fix #6331) #7885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions packages/vitest/src/node/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istan
coverageFiles: CoverageFiles = new Map()
pendingPromises: Promise<void>[] = []
coverageFilesDirectory!: string
roots: string[] = []

_initialize(ctx: Vitest): void {
this.ctx = ctx
Expand Down Expand Up @@ -130,12 +131,19 @@ export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istan
this.options.reportsDirectory,
tempDirectory,
)

// If --project filter is set pick only roots of resolved projects
this.roots = ctx.config.project?.length
? [...new Set(ctx.projects.map(project => project.config.root))]
: [ctx.config.root]
}

/**
* Check if file matches `coverage.include` but not `coverage.exclude`
*/
isIncluded(_filename: string): boolean {
isIncluded(_filename: string, root?: string): boolean {
const roots = root ? [root] : this.roots

const filename = slash(_filename)
const cacheHit = this.globCache.get(filename)

Expand All @@ -144,50 +152,66 @@ export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istan
}

// File outside project root with default allowExternal
if (this.options.allowExternal === false && !filename.startsWith(this.ctx.config.root)) {
if (this.options.allowExternal === false && roots.every(root => !filename.startsWith(root))) {
this.globCache.set(filename, false)

return false
}

const options: pm.PicomatchOptions = {
contains: true,
dot: true,
cwd: this.ctx.config.root,
ignore: this.options.exclude,
}

// By default `coverage.include` matches all files, except "coverage.exclude"
const glob = this.options.include || '**'

const included = pm.isMatch(filename, glob, options) && existsSync(cleanUrl(filename))
let included = roots.some((root) => {
const options: pm.PicomatchOptions = {
contains: true,
dot: true,
cwd: root,
ignore: this.options.exclude,
}

return pm.isMatch(filename, glob, options)
})

included &&= existsSync(cleanUrl(filename))

this.globCache.set(filename, included)

return included
}

async getUntestedFiles(testedFiles: string[]): Promise<string[]> {
if (this.options.include == null) {
return []
}

let includedFiles = await glob(this.options.include, {
cwd: this.ctx.config.root,
private async getUntestedFilesByRoot(
testedFiles: string[],
include: string[],
root: string,
): Promise<string[]> {
let includedFiles = await glob(include, {
cwd: root,
ignore: [...this.options.exclude, ...testedFiles.map(file => slash(file))],
absolute: true,
dot: true,
onlyFiles: true,
})

// Run again through picomatch as tinyglobby's exclude pattern is different ({ "exclude": ["math"] } should ignore "src/math.ts")
includedFiles = includedFiles.filter(file => this.isIncluded(file))
includedFiles = includedFiles.filter(file => this.isIncluded(file, root))

if (this.ctx.config.changed) {
includedFiles = (this.ctx.config.related || []).filter(file => includedFiles.includes(file))
}

return includedFiles.map(file => slash(path.resolve(this.ctx.config.root, file)))
return includedFiles.map(file => slash(path.resolve(root, file)))
}

async getUntestedFiles(testedFiles: string[]): Promise<string[]> {
if (this.options.include == null) {
return []
}

const rootMapper = this.getUntestedFilesByRoot.bind(this, testedFiles, this.options.include)

const matrix = await Promise.all(this.roots.map(rootMapper))

return matrix.flatMap(files => files)
}

createCoverageMap(): CoverageMap {
Expand Down
26 changes: 26 additions & 0 deletions test/coverage-test/fixtures/configs/vitest.config.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
projects: [
{
test: {
name: "project1",
root: "fixtures/workspaces/project/project1",
},
},
{
test: {
name: "project2",
root: "fixtures/workspaces/project/project2",
},
},
{
test: {
name: 'project-shared',
root: 'fixtures/workspaces/project/shared',
}
}
]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { raise } from "../../shared/src/utils"

export const id = <T>(value: T) =>
value ?? raise("Value cannot be undefined")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function untested() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest';
import { id } from '../src/id';

test('returns identity value', () => {
expect(id(1)).toBe(1);
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { raise } from "../../shared/src/utils"

export const konst = <T>(value: T) => {
value ??= raise("Value cannot be undefined")
return () => value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function untested() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from 'vitest'
import { konst } from '../src/konst'

test('returns function that returns constant value', () => {
const fn = konst(1)

expect(fn()).toBe(1);
})
11 changes: 11 additions & 0 deletions test/coverage-test/fixtures/workspaces/project/shared/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function raise(error: string): never
function raise(error: Error): never
function raise(error: Error | string): never {
if (typeof error === 'string') {
throw new Error(error)
} else {
throw error
}
}

export { raise }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'
import { raise } from '../src/utils'

test('raise throws error', () => {
const message = 'Value cannot be undefined'
expect(() => raise(message)).toThrowError(message)
})
49 changes: 49 additions & 0 deletions test/coverage-test/test/workspace.project-filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect } from 'vitest'
import { readCoverageMap, runVitest, test } from '../utils'

test('coverage files include all projects', async () => {
await runVitest({
config: '../../configs/vitest.config.workspace.ts',
coverage: {
reporter: ['json', 'html'],
include: ['**/src/**'],
},
root: 'fixtures/workspaces/project',
})

const coverageMap = await readCoverageMap('fixtures/workspaces/project/coverage/coverage-final.json')
const files = coverageMap.files()

// All files from workspace should be picked
expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/workspaces/project/project1/src/id.ts",
"<process-cwd>/fixtures/workspaces/project/project1/src/untested.ts",
"<process-cwd>/fixtures/workspaces/project/project2/src/konst.ts",
"<process-cwd>/fixtures/workspaces/project/project2/src/untested.ts",
"<process-cwd>/fixtures/workspaces/project/shared/src/utils.ts",
]
`)
})

test('coverage files limited to specified project', async () => {
await runVitest({
config: '../../configs/vitest.config.workspace.ts',
coverage: {
reporter: ['json', 'html'],
include: ['**/src/**'],
},
project: 'project2',
root: 'fixtures/workspaces/project',
})

const coverageMap = await readCoverageMap('fixtures/workspaces/project/coverage/coverage-final.json')
const files = coverageMap.files()

expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/workspaces/project/project2/src/konst.ts",
"<process-cwd>/fixtures/workspaces/project/project2/src/untested.ts",
]
`)
})