Skip to content

Options coverage.include and coverage.exclude do not work as expected #9395

@madeofsun

Description

@madeofsun

Describe the bug

Hello! I am migrating from v3 to v4 and found that new coverage include/exclude logic in v4 does not quite match what was working in v3 and seems like a bug.

Case 1: I want to include all files, but exclude files from top-level

Coverage options:

coverage: {
      include: [
        // all .js,.ts,.tsx files
        '**/*.ts',
      ],
      exclude: [
        // exclude .ts files for top-level
        '*.ts',
      ],
}

Expected behavior: files inside folders are included in coverage and files on top-level - are not

Actual behavior: all files are not included

Potential fix: Do not use contains options for picomatch.isMatch. When I remove it, I achieve expected behavior.

Case 2: I want to exclude all dirs with some particular name even if it happens that CWD already includes such name

Ignore Name: foo

CWD: /Users/iam/code/foo

Coverage options:

coverage: {
      include: [
        // all .js,.ts,.tsx files
        '**/*.ts',
      ],
      exclude: [
        // exclude files in any "foo" folders 
        '**/foo/**',
      ],
}

Expected behavior: all .ts files inside CWD are included in coverage, except files that are under folders with name "foo"

Actual behavior: all files are not included, because CWD is ignored

Potential fix: Use relative filename with picomatch.isMatch. When I use path.relative(process.cwd(), filename) instead of filename, I achieve expected behavior.

Reproduction

Case 1: https://stackblitz.com/edit/vitest-dev-vitest-4nte3ynw?file=vitest.config.ts

Case 2: https://stackblitz.com/edit/vitest-dev-vitest-tguz5bfs?file=vitest.config.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/coverage-istanbul: 4.0.16 => 4.0.16 
    vite: latest => 7.3.0 
    vitest: 4.0.16 => 4.0.16

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: coverageIssues and PRs related to the coverage featurep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions