In an Angular 7 unit test, is there a way to avoid the double async( async(){} )
syntax when combining async support along with the async..await
keywords?
I'm new to angular but am an experienced programmer, and I'm having trouble landing on my preferred test style.
I would like to safely use async..await
in tests, and I understand the below syntax. However, when instructing devs new to modern javascript and/or the concept of async..await
the double async(async())
syntax is redundant and confusing to them. They are leaving out the outer async. Exceptions thrown in the service are causing failures to be reported outside of the actual test which is difficult to track down.
It seems like one of the following would be better:
it()
should magically supportasync..await
and wrap my callback inasync()
so that I don't have to think about it.it()
should take an optional function parameter (i.e.,async
orfakeAsync
) that will wrap my callback.it()
variationsita()
anditfa()
should exist that will wrap my callback with the appropriate async helper.it()
wraps my callback withasync
, and an additionalitf()
will wrap my callback infakeAsync
.
Am I missing an existing concept or syntax? Is there a better alternative?
import { async } from '@angular/core/testing';
describe('MyService', () => {
let service: MyService;
...
it('should get data', async( async() => {
// arrange
let expectedData = { answer: 42 };
// act
let data = await service.getDataAsync();
// assert
expect(data).toEqual(expectedData);
} ));
})
async()
aswaitForAsync()
? – Ivanovo