When to use waitForAsync in angular

痴心易碎 提交于 2020-12-13 03:31:59

问题


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)?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/64384279/when-to-use-waitforasync-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!