When do you use the async function in the TestBed when testing in Angular 2?
When do you use this?
beforeEach(() => {
Te
When you make an async call in your test the actual test function is completed before the async call is completed. When you need to verify some state when the call was completed (which is usually the case) then the test framework would report the test as completed while there is still async work going on.
With using async(...) you tell the test framework to wait until the return promise or observable is completed before treating the test as completed.
it('should show quote after getQuote promise (async)', async(() => {
fixture.detectChanges();
fixture.whenStable().then(() => { // wait for async getQuote
fixture.detectChanges(); // update view with quote
expect(el.textContent).toBe(testQuote);
});
}));
The code passed to then(...) will be executed after the test function itself completed.
With async() you make the test framework aware, that it needs to wait for promises and observables to complete before treating the test as completed.
See also