Skip to content

Repository files navigation

time-mock

build status

browser support

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.jsvarsetTimeout=require("timers").setTimeoutvarnow=require("date-now")module.exports=function(){varitems=[]loop()returnpeekfunctionpeek(){returnitems}functionloop(){items.push(now())setTimeout(loop,1000)}}

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

varmock=require("mock")varassert=require("assert")varTimer=require("../index")// Pass starting time to Timervartimer=Timer(0)// Pass mocked setTimeout and Date.now functions to your modulevartime=mock("./fixtures/time",{timers: {setTimeout: timer.setTimeout},"date-now": timer.now},require)vart=time()console.log("#1",t())assert.deepEqual(t(),[0])timer.advance(500)console.log("#2",t())assert.deepEqual(t(),[0])timer.advance(500)console.log("#3",t())assert.deepEqual(t(),[0,1000])timer.advance(2000)console.log("#4",t())assert.deepEqual(t(),[0,1000,2000,3000])timer.advance(4999)console.log("#5",t())assert.deepEqual(t(),[0,1000,2000,3000,4000,5000,6000,7000])

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

MIT Licenced

About

Easily manipulate and mock out time in your tests

Resources

Stars

3 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages