My goal is to test API calls, taking delays into account. I was inspired by this post.
I've designed a sandbox in which a mock API takes 1000 msec to respond and change the value of a global variable result
. The test checks the value after 500msec and after 1500msec.
Here is the code where the last test is supposed to fail:
let result: number;
const mockAPICall = (delay: number): Observable<number> => {
console.log('API called');
return Observable.of(5).delay(delay);
};
beforeEach(() => {
console.log('before each');
});
it('time test', async(() => {
result = 0;
const delay = 1000;
console.log('start');
mockAPICall(delay).subscribe((apiResult: number) => {
console.log('obs done');
result = apiResult;
});
console.log('first check');
expect(result).toEqual(0);
setTimeout(() => {
console.log('second check');
expect(result).toEqual(0);
}, 500
);
setTimeout(() => {
console.log('third check');
expect(result).toEqual(0);
}, 1500
);
}));
The last test does fail as expected and I get this in the logs:
before each
API called
first check
second check
obs done
third check
Now if I place an async()
in the beforeEach()
:
beforeEach(async(() => {
console.log('async before each');
}));
, the test passes and I only get this in the logs:
async before each
API called
first check
I didn't expect that. Why this behavior ? What happens behind the scenes ?
Note: I'll need this async()
in beforeEach()
in future tests because I'll use a testBed
and compileComponents
.