Let's say I have an Angular Component which defines an Observable myObs$
as one of its properties.
In one test, given certain conditions, I want to test that myObs$
does not notify. In the logic there are some delays involved, so the test must be asynchronous.
I am using Jasmine.
So far I have been able to craft this solution:
it('async test', done => {
let dataEmitted;
myObs$
.pipe(
tap(data => dataEmitted = data),
)
.subscribe();
setTimeout(() => {
if (dataEmitted) {
done.fail('should not emit');
} else {
done();
}
}, 1000);
});
But I am far from happy with it. I have to rely on setTimeout
to perform the checks and call the done
function.
Any suggestions on how to perform such tests properly? Synchronous solutions do not work since there is intrinsic asynchronicity in the logic.