Skip to content

Coverage ignore comments don't work for Vitest #32813

@Naitronbot

Description

@Naitronbot

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When adding v8 coverage ignore comments (Vitest docs), generated coverage reports still include suppressed lines. This only happens with the Vitest runner, when using the Karma runner, istanbul ignore comments work as expected.

Minimal Reproduction

  1. Create a new project: ng new Test
    • cd Test
  2. Install Vitest coverage: npm i -D @vitest/coverage-v8
  3. Add uncovered branch with ignore comment to src/app/app.ts
export class App {
  protected readonly title = signal('Test');


+ constructor() {
+   /* v8 ignore if -- @preserve */
+   if (this.title() === 'Not Test') {
+     console.log('I dont get run!');
+   }
+ }
}
  1. Run test with coverage ng test --coverage
    Observe ignored code is still marked as uncovered
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |   98.24 |     92.3 |     100 |   97.82 |
 app.html |     100 |      100 |     100 |     100 |
 app.ts   |    87.5 |     92.3 |     100 |      75 | 16
----------|---------|----------|---------|---------|-------------------
  1. Switch runner to Karma in angular.json
         "test": {
-          "builder": "@angular/build:unit-test"
+          "builder": "@angular/build:unit-test",
+          "options": {"runner": "karma"}
         }
  1. Install Karma dependencies: npm i -D karma karma-jasmine karma-coverage karma-chrome-launcher karma-jasmine-html-reporter
  2. Change coverage ignore comment to istanbul in src/app/app.ts
<       /* v8 ignore if -- @preserve */
---
>       /* istanbul ignore if -- @preserve */
  1. Run tests with coverage ng test --coverage
  2. Observe coverage report correctly ignores suppressed lines
=============================== Coverage summary ===============================
Statements   : 100% ( 53/53 )
Branches     : 100% ( 6/6 )
Functions    : 100% ( 9/9 )
Lines        : 100% ( 45/45 )
================================================================================

Exception or Error


Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI       : 21.2.3
Angular           : 21.2.5
Node.js           : 25.8.1 (Unsupported)
Package Manager   : npm 11.11.0
Operating System  : linux x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.2.3            │ ^21.2.3           │
│ @angular/cli              │ 21.2.3            │ ^21.2.3           │
│ @angular/common           │ 21.2.5            │ ^21.2.0           │
│ @angular/compiler         │ 21.2.5            │ ^21.2.0           │
│ @angular/compiler-cli     │ 21.2.5            │ ^21.2.0           │
│ @angular/core             │ 21.2.5            │ ^21.2.0           │
│ @angular/forms            │ 21.2.5            │ ^21.2.0           │
│ @angular/platform-browser │ 21.2.5            │ ^21.2.0           │
│ @angular/router           │ 21.2.5            │ ^21.2.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.1.0             │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions