Timer Mocks
Learn how to effectively mock and leverage timers in Jest.
Why do we mock timers?
Our discussion on mocking timers largely covers two use cases. The first is mocking timers in our code for efficiency reasons. The second is mocking for the same reasons that we often mock functions: we want to be able to watch them and assert on how timers were called and run.
Mocking for efficiency
Timers, by nature, take time—a resource that we are constantly looking to optimize and reduce in our test runs. We don’t necessarily need our timers to run through their entirety in our test runs. At times we may want to, but we often want to hit the timer and keep running. Mocking the timer at hand allows us to do this.
Mocking for deeper insights
At other times, we don’t need to speed up the execution. Instead, we want to gain ...