How to test a function which has a setTimeout with jasmine?

后端 未结 4 1563
失恋的感觉
失恋的感觉 2020-12-02 22:00

I need to write a test for a function that has a setTimeout() call inside, but i can\'t find how i should do.

This is the function

// Di         


        
4条回答
  •  伪装坚强ぢ
    2020-12-02 22:40

    For anyone googling this, a better answer can be found timer testing

    import { fakeAsync, tick, discardPeriodicTasks } from '@angular/core/testing';
    
    it('polls statusStore.refreshStatus on an interval', fakeAsync(() => {
      spyOn(mockStatusStore, 'refreshStatus').and.callThrough();
      component.ngOnInit();
      expect(mockStatusStore.refreshStatus).not.toHaveBeenCalled();
      tick(3001);
      expect(mockStatusStore.refreshStatus).toHaveBeenCalled();
      tick(3001);
      expect(mockStatusStore.refreshStatus).toHaveBeenCalledTimes(2);
      discardPeriodicTasks();
     }));
    

提交回复
热议问题