Why is jest.useFakeTimers not working with RxJs Observable delay

前端 未结 1 655
甜味超标
甜味超标 2020-12-19 18:48

I\'m wondering why jest.useFakeTimers is working with setTimeout but not with the delay operator of RxJs:

jest.useFakeTimers();
imp         


        
1条回答
  •  执笔经年
    2020-12-19 19:39

    The delay operator does not work with Jest's fake timers because the delay operator's implementation uses its scheduler's concept of time - which is unrelated to Jest's concept of fake time.

    The source is here:

    while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) {
      queue.shift().notification.observe(destination);
    }
    

    The current (non-fake) time is assigned to notifications when they are created and the current time is what the scheduler's now method returns. When Jest's fake timers are used, an insufficient amount of actual (non-fake) time will have elapsed and the notifications will remain in the queue.

    To write RxJS tests using fake or virtual time, you can use the VirtualTimeScheduler. See this answer. Or you can use the TestScheduler and marble tests.

    0 讨论(0)
提交回复
热议问题