I am attempting to setup a test of a setInterval call. setInterval always creates a new timer so no matter how far ahead you move tick() there will always be an error with this message:
Error: 1 timer(s) still in the queue.
If I override the setTimeout function with setInterval, then the test passes as expected. I have added code to allow this to my angular service using a public property myTimerFunc that is normally set to setInterval. The test code sets it to setTimeout.
Is there a better way to test setInterval?
It would be nice if the last timer in the queue would be ignored by jasmine. Then I could test without extra cruft.
For example this code will fail unless you uncomment the line that overrides setInterval. I do not mind doing this in the test code but I do want to avoid adding extra stuff to the normal code just to allow this override.
import { fakeAsync, tick } from '@angular/core/testing'
describe('testing setTimeout and setInterval:', () => {
it('setTimeout should work', fakeAsync(() => {
let callback = jasmine.createSpy('setTimeoutCallback')
setTimeout(() => {
callback()
}, 55)
tick(56)
expect(callback).toHaveBeenCalled();
}));
// let setInterval = setTimeout
it('setInterval should work', fakeAsync(() => {
let callback = jasmine.createSpy('setTimeoutCallback')
setInterval(() => {
callback()
}, 55)
tick(56)
expect(callback).toHaveBeenCalled();
}));
})