33

From documentation we can read:

waitForAsync(fn: Function): (done: any) => any

Wraps a test function in an asynchronous test zone. The test will automatically complete when all asynchronous calls within this zone are done. Can be used to wrap an inject call.

I could not understand, when to use waitForAsync function? What's the difference between waitForAsync vs (async or fakeAsync)?

Thomas Banderas
  • 1,681
  • 1
  • 21
  • 43
  • Does this answer your question? [What is the difference between fakeAsync and async in angular2 testing](https://stackoverflow.com/questions/42971537/what-is-the-difference-between-fakeasync-and-async-in-angular2-testing) – Bozhinovski Oct 16 '20 at 07:05
  • 1
    Hey, sorry but no It's a good point, but the main question is for a `waitForAsync` method which is not described in article which you posted – Thomas Banderas Oct 16 '20 at 07:08

2 Answers2

41

In Angular 10.1.0, waitForAsync() has replaced async() to avoid confusion, but is otherwise exactly the same. Any documentation you see that discusses using async() will also apply to waitForAsync(). async() has been marked as deprecated and will be removed entirely in version 12.

seairth
  • 1,966
  • 15
  • 22
8

Wraps a test function in an asynchronous test zone. The test will automatically complete when all asynchronous calls within this zone are done. Can be used to wrap an inject call.

So you dont have to manually call done() callback passed as an argument to mark test had finished or use fakeAsync() and other helper functions from '@angular/core/testing'

it('...', waitForAsync(inject([AClass], (object) => {
  object.doSomething.then(() => {
    expect(...);
  })
});

See docs.

Felix
  • 3,999
  • 3
  • 42
  • 66