SkillByAIOpen interactive version →

Lesson 30 / 32

Testing a Component

Test components in isolation with TestBed, change detection and assertions on component state.

What TestBed does

TestBed creates a mini Angular module for the test, letting you instantiate a component with real or fake dependencies and inspect its rendered output.

A minimal test

This test creates the component, triggers change detection, and checks a value on the instance.

describe('CounterComponent', () => {
  it('increments the count', () => {
    const fixture = TestBed.createComponent(CounterComponent);
    const component = fixture.componentInstance;

    component.increment();
    fixture.detectChanges();

    expect(component.count()).toBe(1);
  });
});