MockTimers

external class MockTimers(source)

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

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
fun enable(options: MockTimersOptions = definedExternally)

Enables timer mocking for the specified timers.

Link copied to clipboard
fun reset()

This function restores the default behavior of all mocks that were previously created by this MockTimers instance and disassociates the mocks from the MockTracker instance.

Link copied to clipboard
fun runAll()

Triggers all pending mocked timers immediately. If the Date object is also mocked, it will also advance the Date object to the furthest timer's time.

Link copied to clipboard
fun setTime(time: Number)

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.

Link copied to clipboard
fun tick(milliseconds: Number)

Advances time for all mocked timers.