Mocking time in Jasmine specs

Posted . Visible to the public.

The easiest way to freeze or travel through time in a Jasmine spec is to use the built-in jasmine.clock() Show archive.org snapshot .

  • After jasmine.clock().install() you can use it to control setTimeout and setInterval.
  • Using jasmine.clock().mockDate() you can mock new Date() (which returns the current time in Javascript)

While you can use SinonJS Fake timers Show archive.org snapshot , using the built-in Jasmine clock will save you an extra dependency.

Henning Koch
Last edit
Henning Koch
Keywords
javascript, tests, Time.now, Time.current
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2015-07-29 18:41)