Testing Asynchronous Services

Learn how to test asynchronous services using the waitForAsync and fakeAsync artifacts.

Angular testing utilities

Angular testing utilities provide two artifacts to tackle asynchronous testing scenarios:

  • waitForAsync: An asynchronous approach to unit test asynchronous services. It is combined with the whenStable method of ComponentFixture.

  • fakeAsync: A synchronous approach to unit test asynchronous services. It is used in combination with the tick function.

Both approaches provide roughly the same functionality; they only differ in how we use them. Let’s see how we can use each one by looking at an example. Consider the following async.component.ts file:

Get hands-on with 1400+ tech skills courses.