vendredi 10 juillet 2020

Why is my setInterval Jasmine test not detecting more than one execution?

I have a setInterval that is calling a function refreshData() every X seconds.

 this.timerId = setInterval(this.refreshData, this.refreshInterval);

Using a console.log() in the refreshData(), I checked that it is indeed called every X seconds.

I tried to test this using Jasmine

 it('should set an interval', () => {
    jasmine.clock().install();
    expect(component.refreshData).toHaveBeenCalled();
    jasmine.clock().tick(component.refreshInterval*2);
    expect(component.refreshData).toHaveBeenCalledTimes(2);
    jasmine.clock().uninstall();
  });

No matter the value I pass as a parameter in tick(), I get the error that it is called only once.

Is my test not written properly or is my component not behaving as I expect it to ?

Aucun commentaire:

Enregistrer un commentaire