A simple mock for
requestAnimationFrame testing with fake timers.
Adapted with gratitude from
To use with
mockRaf instance, exposing the functions you'll use to interact with the mock.
const mockRaf =
Returns the current
now value of the mock. Starts at 0 and increases with each
step() taken. Useful for stubbing out
performance.now() or a polyfill when using
requestAnimationFrame with timers.
requestAnimationFrame or a polyfill. Adds a callback to be fired on the next step.
cancelAnimationFrame or a polyfill. Removes all currently scheduled
requestAnimationFrame callbacks from the queue.
Continuously iterate the
requestAnimationFrame queue until empty.
Useful for jumping to the end of an animation (or group of animations).
requestAnimationFrame steps. Fires currently queued callbacks for each step and increments
now time for each step. The primary way to interact with a
mockRaf instance for testing.
step() takes an optional
1000 / 60
The time that should pass during each
requestAnimationFrame step in milliseconds. Default is roughly equivalent to default browser behavior.
The number of steps to take.