In an angular application, I use some tools library and some of my code that have:
- Async declaration
- Use a setInterval inside that I don't want to wait.
I've found this article that shows how to use fakeAsync, flushMicrotask and tick, that seem to do the job, but it doesn't seems to work.
Currently I've this:
it('should refresh the token in time', fakeAsync(() => {
//Arrange
let token = null;
generateToken().then(result => {
token = result;
console.log('Generated token: ' + token); //properly displayed
});
flushMicrotasks();
expect(token).not.toBeNull(); //crashes
//Rest of the test
}));
});
but it seems the expect
is executed before the generateToken
method.
(generateToken
returns a Promise and basically generate a test token with jose library)
I could use the async declaration in the "it" method, but my understanding is that won't solve my real issue, to accelerate a setInterval
Has the recommended way of testing async code changed? Or what am I missing?