Skip to content

Coverage in library unit-test includes dependent library files and does not reduce runtime when using coverage.include #33484

Open

Description

@Carlosamouco

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 running unit tests for a library with @angular/build:unit-test, the coverage report includes files from other workspace libraries that the tested library depends on, which is unexpected.

Although this can be restricted with coverage.include, coverage execution time remains unchanged.

Minimal Reproduction

  1. Create two Angular libraries in the workspace (e.g. lib-a and lib-b)
  2. Make lib-a depend on lib-b
  3. Add a unit test in lib-a
  4. Run tests with coverage enabled:
ng test lib-a --coverage

Observed behavior

  • The coverage report contains files from lib-b, even though only lib-a is being tested.
  • Restricting coverage.include changes the generated report but does not noticeably reduce execution time.

Performance

Times below are for a single library test run inside a larger Angular monorepo where the tested library depends on ~7 other libraries from the same workspace.
Execution times vary depending on configuration (development vs production) used to build each of the libraries.

Without coverage:

Duration 24.94s
(transform 62.28s, setup 6.91s, import 98.85s, tests 982ms, environment 13.89s)

With coverage (production build):

Duration 154.60s
(transform 78.24s, setup 13.24s, import 135.76s, tests 1.58s, environment 19.90s)

With coverage (development build)

Duration 201.20s
(transform 57.83s, setup 10.09s, import 99.85s, tests 1.25s, environment 15.00s)

Your Environment

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

Angular CLI       : 22.0.4
Angular           : 22.0.4
Node.js           : 24.16.0
Package Manager   : npm 11.13.0
Operating System  : linux x64

┌───────────────────────────────────┬───────────────────┬───────────────────┐
│ Package                           │ Installed Version │ Requested Version │
├───────────────────────────────────┼───────────────────┼───────────────────┤
│ @angular/animations               │ 22.0.4            │ ^22.0.4           │
│ @angular/build                    │ 22.0.4            │ ^22.0.4           │
│ @angular/cli                      │ 22.0.4            │ ^22.0.4           │
│ @angular/common                   │ 22.0.4            │ ^22.0.4           │
│ @angular/compiler                 │ 22.0.4            │ ^22.0.4           │
│ @angular/compiler-cli             │ 22.0.4            │ ^22.0.4           │
│ @angular/core                     │ 22.0.4            │ ^22.0.4           │
│ @angular/forms                    │ 22.0.4            │ ^22.0.4           │
│ @angular/localize                 │ 22.0.4            │ ^22.0.4           │
│ @angular/platform-browser         │ 22.0.4            │ ^22.0.4           │
│ @angular/platform-browser-dynamic │ 22.0.4            │ ^22.0.4           │
│ @angular/router                   │ 22.0.4            │ ^22.0.4           │
│ @angular/service-worker           │ 22.0.4            │ ^22.0.4           │
│ ng-packagr                        │ 22.0.0            │ ^22.0.0           │
│ rxjs                              │ 7.8.2             │ ~7.8.2            │
│ typescript                        │ 6.0.3             │ ^6.0.3            │
│ vitest                            │ 4.1.9             │ ^4.1.9            │
│ zone.js                           │ 0.16.2            │ ~0.16.2           │
└───────────────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

No response

Activity

  1. added this to the needsTriage milestone on Jun 30, 2026
  2. changed the title [-]`unit-test` performance degradation when enabling coverage for angular libraries[/-] [+]Coverage in library `unit-test` includes dependent library files and does not reduce runtime when using coverage.include[/+] on Jun 30, 2026
  3. nagahara-naoki commented on Nov 8, 2026

    @nagahara-naoki

    Thanks for reporting this! I tried a small two-library example and could reproduce the report-scope behavior with Angular CLI / @angular/build 22.0.4 and Vitest / @vitest/coverage-v8 4.1.9 (Node 24.22.2, macOS arm64, jsdom).

    What I checked

    1. Generate lib-a and lib-b using the Angular CLI library schematic.
    2. Build lib-b with ng build lib-b --configuration development, keeping the generated lib-b path mapping to dist/lib-b.
    3. Import LibB from lib-b in the LibA component and render it in the template:
    import { Component } from '@angular/core';
    import { LibB } from 'lib-b';
    
    @Component({
      selector: 'lib-lib-a',
      imports: [LibB],
      template: '<lib-lib-b />',
    })
    export class LibA {}
    1. Run only the generated lib-a test:
    ng test lib-a --watch=false --coverage --coverage-reporters=json

    The single lib-a test passes, but coverage/lib-a/coverage-final.json contains both:

    projects/lib-a/src/lib/lib-a.ts
    projects/lib-b/src/lib/lib-b.ts
    

    Explicitly restricting the coverage scope excludes lib-b:

    ng test lib-a --watch=false --coverage --coverage-reporters=json \
      --coverage-include='projects/lib-a/src/**/*.ts'

    I have not reproduced the substantial slowdown in this small example. Three runs per configuration took roughly two seconds each, with no clear improvement from restricting coverage. This is only a small smoke comparison, not a performance benchmark. I also have not yet checked the latest release or repository main.

    Question about the intended behavior

    Is the default coverage scope for a library expected to include executed code from dependent workspace libraries, or should it be limited to the library being tested? Since lib-b is executed by the test, I wanted to clarify whether this is intended behavior before proposing a change.

    If the intended default is project-local coverage, one possible direction would be to derive the default include pattern from the tested project's sourceRoot, while preserving explicit coverageInclude / runner configuration overrides. That would address the report scope; the performance concern would still need separate investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions