Skip to main content
The Clock class allows you to control time in your tests by manipulating browser timers. This is useful for testing time-dependent behavior without actually waiting.

Overview

Access the clock through the browser context:

Methods

install

Installs fake timers and controls time.
number | string | Date
Time to initialize the clock. Can be:
  • number: Unix timestamp in milliseconds
  • string: Date string
  • Date: Date object
If not specified, the clock starts at the current time.
Returns: Promise<void>

fastForward

Advances the clock by the specified amount of time.
number | string
required
Amount of time to advance. Can be:
  • number: Time in milliseconds
  • string: Duration string (e.g., ‘01:00:00’ for 1 hour)
Returns: Promise<void>

pauseAt

Pauses time at the specified moment.
number | string | Date
required
Time to pause at. Can be:
  • number: Unix timestamp in milliseconds
  • string: Date string
  • Date: Date object
Returns: Promise<void>

resume

Resumes normal time flow after it was paused.
Returns: Promise<void>

runFor

Advances the clock by running all pending timers for the specified duration.
number | string
required
Amount of time to run. Can be:
  • number: Time in milliseconds
  • string: Duration string (e.g., ‘00:05:00’ for 5 minutes)
Returns: Promise<void>

setFixedTime

Sets the clock to a fixed time. All subsequent time queries return this time.
number | string | Date
required
Time to fix the clock at. Can be:
  • number: Unix timestamp in milliseconds
  • string: Date string
  • Date: Date object
Returns: Promise<void>

setSystemTime

Sets the current system time without affecting timers.
number | string | Date
required
Time to set. Can be:
  • number: Unix timestamp in milliseconds
  • string: Date string
  • Date: Date object
Returns: Promise<void>

Examples

Testing time-dependent UI

Testing countdown timer

Testing scheduled tasks

Pausing and resuming time