Performance: Component testing basics
Component testing affects development speed and runtime performance by ensuring components render efficiently and handle inputs without unnecessary re-renders.
Jump into concepts and practice - no test required
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MyComponent } from './my.component'; describe('MyComponent', () => { let fixture: ComponentFixture<MyComponent>; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [MyComponent] }).compileComponents(); fixture = TestBed.createComponent(MyComponent); }); it('should render and update input efficiently', () => { fixture.componentInstance.inputValue = 'test'; fixture.detectChanges(); expect(fixture.nativeElement.textContent).toContain('test'); }); });
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { MyComponent } from './my.component'; describe('MyComponent', () => { let fixture: ComponentFixture<MyComponent>; beforeEach(() => { TestBed.configureTestingModule({ declarations: [MyComponent] }).compileComponents(); fixture = TestBed.createComponent(MyComponent); }); it('should render and update input', () => { fixture.componentInstance.inputValue = 'test'; fixture.detectChanges(); expect(fixture.nativeElement.textContent).toContain('test'); }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full TestBed setup with multiple detectChanges | High (many DOM nodes) | Multiple reflows per test | High paint cost due to full DOM updates | [X] Bad |
| Minimal TestBed with async compileComponents and single detectChanges | Low (only necessary nodes) | Single reflow per test | Low paint cost with targeted updates | [OK] Good |
fixture.nativeElement.textContent contain?TestBed.configureTestingModule({ declarations: [HelloComponent] }).compileComponents();
const fixture = TestBed.createComponent(HelloComponent);
fixture.componentInstance.name = 'Alice';
fixture.detectChanges();
@Component({ selector: 'app-hello', template: '<p>Hello, {{name}}!</p>' })
class HelloComponent { name = ''; }name property is set to 'Alice' before change detection.fixture.detectChanges()name value, so the paragraph shows 'Hello, Alice!'.beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MyComponent]
});
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});compileComponents() must be called to compile templates before creating the component.compileComponents(), which can cause errors or incomplete setup.@Component({ template: '<ul><li *ngFor="let item of items">{{item}}</li></ul>' })
class ListComponent { @Input() items: string[] = []; }component.items and call detectChanges() to update the rendered list.<li> elements in the DOM matches the input array length using querySelectorAll('li').length.