Skip to content

Using TestBed.overrideComponent to mock dependencies breaks code coverage #67383

@stanrogo

Description

@stanrogo

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

No

Description

We mock the dependencies of our standalone components using TestBed.overrideComponent with vitest and v8-coverage.

As soon as we do this, the HTML coverage drops. It's as if the coverage provider thinks that the component is not being used at all because it's not the "real" component anymore, but an overriden version.

In the attached example repo (minimal reproduction), coverage is 100% when not using TestBed.overrideComponent:

  it('should create the app', async () => {
    TestBed.configureTestingModule({
      imports: [App],
    });
    const fixture = TestBed.createComponent(App);
    await fixture.whenStable();
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
Image

After overriding the component to mock the child:

  it('should still provide all code coverage', async () => {
    TestBed.configureTestingModule({
      imports: [App],
    }).overrideComponent(App, {
      set: {
        imports: [MockAppChild],
      },
    });

    const fixture = TestBed.createComponent(App);
    await fixture.whenStable();
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });
Image

Please provide a link to a minimal reproduction of the bug

https://github.com/stanrogo/repro-angular-override-component-coverage

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI       : 21.2.0
Angular           : 21.2.0
Node.js           : 20.20.0
Package Manager   : npm 11.8.0
Operating System  : win32 x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.2.0            │ 21.2.0            │
│ @angular/cli              │ 21.2.0            │ 21.2.0            │
│ @angular/common           │ 21.2.0            │ 21.2.0            │
│ @angular/compiler         │ 21.2.0            │ 21.2.0            │
│ @angular/compiler-cli     │ 21.2.0            │ 21.2.0            │
│ @angular/core             │ 21.2.0            │ 21.2.0            │
│ @angular/platform-browser │ 21.2.0            │ 21.2.0            │
│ rxjs                      │ 7.8.2             │ 7.8.2             │
│ typescript                │ 5.9.3             │ 5.9.3             │
│ vitest                    │ 4.0.18            │ 4.0.18            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testingIssues related to Angular testing features, such as TestBedgemini-triagedLabel noting that an issue has been triaged by gemini

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions