Performance: Component testing basics
MEDIUM IMPACT
Component testing affects development speed and runtime performance by ensuring components render efficiently and handle inputs without unnecessary re-renders.
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 |