time-mock
Easily manipulate and mock out time in your tests
Example
Let's say you have a simple time module that collects an array of timestamps every second.
// ./fixtures/time.jsvar setTimeout = setTimeoutvar now = module { var items = return peek { return items } { items }}
It uses require("timers").setTimeout
and require("date-now")
so that it's
not hardcoded to time based global state.
We can then simply mock these things out using mock
var mock = var assert = var Timer = // Pass starting time to Timervar timer = // Pass mocked setTimeout and Date.now functions to your modulevar time = var t = consoleassert timer consoleassert timer consoleassert timer consoleassert timer consoleassert
Timer basically allows you to create simple mockable functions for setTimeout
and Date.now. You can then call timer.advance(delta)
to make time move
forward.
This is awesome for unit tests when you don't want your tests to be slow based on the fact that they have to wait for timeouts.
Installation
npm install time-mock
Contributors
- Raynos