Testing Signals and Async Code
Test reactive and asynchronous behavior.
Async Is the Hard Part of Testing
Real components do async work: timers, promises, HTTP, debounced inputs. Tests must control time so assertions run after the async work settles. Angular gives you fakeAsync, tick, and flush for deterministic time control.
Testing a Signal
A signal is read like a function. Testing it is straightforward: call the signal, assert the value; update it and assert again.
it('increments the count signal', () => {
const fixture = TestBed.createComponent(CounterComponent);
const c = fixture.componentInstance;
expect(c.count()).toBe(0);
c.increment();
expect(c.count()).toBe(1);
});All lessons in this course
- TestBed and Component Fixtures
- Testing Inputs, Outputs, and DOM
- Mocking Services and Dependencies
- Testing Signals and Async Code