MockTimers
Mocking timers is a technique commonly used in software testing to simulate and control the behavior of timers, such as setInterval
and setTimeout
, without actually waiting for the specified time intervals.
The MockTimers API also allows for mocking of the Date
constructor and setImmediate
/clearImmediate
functions.
The MockTracker
provides a top-level timers
export which is a MockTimers
instance.
Since
v20.4.0
Functions
Link copied to clipboard
Enables timer mocking for the specified timers.
Link copied to clipboard
You can use the .setTime()
method to manually move the mocked date to another time. This method only accepts a positive integer. Note: This method will execute any mocked timers that are in the past from the new time. In the below example we are setting a new time for the mocked date.