Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Regression TestBed.overrideComponent in Angular 17 #52817

@satanTime

Description

@satanTime

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

core

Is this a regression?

Yes

Description

TestBed.overrideComponent doesn't override standalone components if the components have been used in other tests before. Might be related to #48432.

To reproduce, run the next test in any Angular 17 project.
It looks like there is a race, something isn't awaited properly.
The first test is skipped with x, remove x to reproduce the issue.
If you've got success, simply add x back and remove it again.
In this case, you can see that the 2nd test fails time to time because overrideComponent wasn't applied.

import { TestBed } from '@angular/core/testing';
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: '<router-outlet></router-outlet>',
  standalone: true,
  template: 'mock',
})
export class MockRouterOutlet {}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet],
  template: '<router-outlet></router-outlet>',
})
export class AppComponent {
  title = 'a17';
}

describe('AppComponent with real router', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [AppComponent],
    }).compileComponents();
  });

  // REMOVE "x" TO REPRODUCE THE ISSUE
  xit('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    expect(fixture.nativeElement.innerHTML).toEqual('<router-outlet></router-outlet><!--container-->');
  });
});

describe('AppComponent with mock router', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [AppComponent],
    }).overrideComponent(AppComponent, {
      set: {
        imports: [MockRouterOutlet],
      }
    }).compileComponents();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    expect(fixture.nativeElement.innerHTML).toEqual('<router-outlet>mock</router-outlet>');
  });
});

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No errors, `TestBed.overrideComponent` doesn't simply do anything.

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

ngular CLI: 17.0.0
Node: 20.9.0
Package Manager: npm 10.1.0
OS: darwin x64

Angular: 17.0.2
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1700.0
@angular-devkit/build-angular   17.0.0
@angular-devkit/core            17.0.0
@angular-devkit/schematics      17.0.0
@angular/cli                    17.0.0
@schematics/angular             17.0.0
rxjs                            7.8.1
typescript                      5.2.2
zone.js                         0.14.2

Anything else?

No response

Metadata

Metadata

Assignees

Labels

P2The issue is important to a large percentage of users, with a workaroundarea: testingIssues related to Angular testing features, such as TestBedregressionIndicates than the issue relates to something that worked in a previous versionstate: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions