This guide describes testing utilities primarily used for managing and controlling async tasks in unit tests. These utilities are essentially the Zone.js-specific mock clock utilities, particularly relevant for controlling the flow of asynchronous operations within tests.
For general Angular testing utilities, including TestBed and ComponentFixture, see the Testing Utility APIs guide.
Here's a summary of Zone.js-specific functions:
| Function | Details |
|---|---|
waitForAsync |
Tracks async tasks and completes the tests only once there are no longer any micro or macrotasks remaining in the test zone. See waitForAsync. |
fakeAsync |
Runs the body of a test (it) within a special fakeAsync test zone, enabling a linear control flow coding style. See fakeAsync. |
tick |
Simulates the passage of time and the completion of pending asynchronous activities by flushing both timer and micro-task queues within the fakeAsync test zone. The curious, dedicated reader might enjoy this lengthy blog post, "Tasks, microtasks, queues and schedules". Accepts an optional argument that moves the virtual clock forward by the specified number of milliseconds, clearing asynchronous activities scheduled within that timeframe. See tick. |
discardPeriodicTasks |
Discards any periodic tasks (e.g. setInterval) that were created inside the fakeAsync Zone. |
flushMicrotasks |
When a fakeAsync() test ends with pending micro-tasks such as unresolved promises, the test fails with a clear error message. In general, a test should wait for micro-tasks to finish. When pending microtasks are expected, call flushMicrotasks to flush the micro-task queue and avoid the error. |