Class: Clock

Clock

Jasmine's mock clock is used when testing time dependent code.
Note: Do not construct this directly. You can get the current clock with jasmine.clock.

Since:
  • 1.3.0

Methods

autoTick()

Updates the clock to automatically advance time.

With this mode, the clock advances to the first scheduled timer and fires it, in a loop. Between each timer, it will also break the event loop, allowing any scheduled promise callbacks to execute before running the next one.

This mode allows tests to be authored in a way that does not need to be aware of the mock clock. Consequently, tests which have been authored without a mock clock installed can one with auto tick enabled without any other updates to the test logic.

In many cases, this can greatly improve test execution speed because asynchronous tasks will execute as quickly as possible rather than waiting real time to complete.

Furthermore, tests can be authored in a consitent manner. They can always be written in an asynchronous style rather than having tick sprinkled throughout the tests with mock time in order to manually advance the clock.

When auto tick is enabled, tick can still be used to synchronously advance the clock if necessary.

Since:
  • 5.7.0

install() → {Clock}

Install the mock clock over the built-in methods.

Since:
  • 2.0.0
Returns:
Type
Clock

mockDate(initialDateopt)

Instruct the installed Clock to also mock the date returned by new Date()

Parameters:
Name Type Attributes Default Description
initialDate Date <optional>
now

The Date to provide.

Since:
  • 2.1.0

tick(millis)

Tick the Clock forward, running any enqueued timeouts along the way

Parameters:
Name Type Description
millis int

The number of milliseconds to tick.

Since:
  • 1.3.0

uninstall()

Uninstall the mock clock, returning the built-in methods to their places.

Since:
  • 2.0.0

withMock(closure)

Execute a function with a mocked Clock

The clock will be installed before the function is called and uninstalled in a finally after the function completes.

Parameters:
Name Type Description
closure function

The function to be called.

Since:
  • 2.3.0