Sub topic 12
Testing zoneless components
This chapter assumes you can write standalone components with signals and OnPush, and that you use Jasmine or Jest-style describe/it tests with TestBed. You should also know what provideZonelessChangeDetection() does in an application.
Why tests change without zones
In a zone-based test, Zone.js patches events and timers. After a click, Angular knew something might have changed. Tests could then call fixture.detectChanges() and read the DOM.
A zoneless app has no such patching. A view updates only when Angular is told to check it, and that happens through signals, markForCheck(), the async pipe, or template events. Your tests should use the same path as production. A test that forces a check by hand can pass while the real app never updates.
Enable zoneless mode in TestBed
Add the provider to the testing module, then wait for stability instead of forcing a check.
import { TestBed } from '@angular/core/testing';
import { provideZonelessChangeDetection } from '@angular/core';
import { Counter } from './counter';
describe('Counter', () => {
it('updates the view after a click', async () => {
TestBed.configureTestingModule({
providers: [provideZonelessChangeDetection()],
});
const fixture = TestBed.createComponent(Counter);
await fixture.whenStable();
fixture.nativeElement.querySelector('button').click();
await fixture.whenStable();
expect(fixture.nativeElement.textContent).toContain('Clicked 1 times');
});
});The first await fixture.whenStable() renders the initial view. The click updates the count signal, which schedules a change detection run. The second await resolves after that run finishes, so the assertion sees the new text.
Prefer whenStable over detectChanges
fixture.detectChanges() still exists, but it forces a check regardless of whether Angular was notified. That can hide a bug. Suppose a component changes a plain field inside a timer and never calls markForCheck(). The app would show stale data, but a test that calls detectChanges() would pass.
With await fixture.whenStable(), the same test fails, because no notification scheduled a check. That failure is the signal you want. The fix is to move the state into a signal or call markForCheck().
Async work and stability
whenStable() waits for the application's pending tasks. Angular tracks its own work, such as HttpClient requests, automatically. For your own asynchronous work, register it with PendingTasks, so tests and server rendering know to wait.
import { Injectable, inject, PendingTasks } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class DataLoader {
private readonly pendingTasks = inject(PendingTasks);
load(url: string) {
return this.pendingTasks.run(async () => {
const response = await fetch(url);
return response.json();
});
}
}A test that triggers load() and then awaits whenStable() will wait for the fetch to finish. Without PendingTasks, whenStable() can resolve too early, and you'd see the loading state in your assertion.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Assertion sees old text | State is a plain field and nothing notified Angular | Use a signal or call `markForCheck()` |
| Test passes only with `detectChanges()` | The test hides a missing notification | Replace it with `await fixture.whenStable()` |
| Data appears after the assertion runs | Custom async work is not tracked | Wrap it in `PendingTasks.run()` |
| Test passes but the app fails | Tests still use zone-based providers | Add `provideZonelessChangeDetection()` |
Exercise
A component increments a plain count field in a setTimeout and never calls markForCheck(). Your test calls fixture.detectChanges() after the timer and passes. Why is this a problem, and what should the test await instead?
Show answer
detectChanges() forces a check, so it hides the fact that nothing told Angular the view was dirty. In the real zoneless app the view would stay stale. The test should use await fixture.whenStable(), which fails because no check was scheduled. The component should then store count in a signal or call markForCheck().