Skip to content
Fake timers API for testdouble.js.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.babelrc
.gitignore
.npmignore
.travis.yml
README.md
package.json

README.md

NPM Package Travis Dependency Status

testdouble-timers

Fake timers API for testdouble.js.

Install

npm install --save-dev testdouble-timers

Usage

import td from 'testdouble';
import timers from 'testdouble-timers';

// Install fake timers API to testdouble
timers.use(td);

describe('important scenario', () => {
  afterEach(() => {
    // Recommended way to restore replaced methods.
    td.reset();
  });

  it('does something later', () => {
    // Replace global timers
    const clock = td.timers();

    obj.doLater(500);

    assert(typeof obj.result === 'undefined');

    // Forward 510 msec
    clock.tick(510);

    assert(obj.result === 'hello');

    // No need to call clock.restore() here
  });
});

API

testdouble-timers has Sinon.JS compatible API.

td.timers([now])

td.timers(method1, method2, ...)

td.timers(now, method1, method2, ...)

Creates a new clock and replaces all methods if you call without any parameters.

now should be Date object or milliseconds since UNIX epoch. method1, method2, ... are method names you want to replace. Here is a list of method names you can specify.

List of available method names

  • setTimeout
  • clearTimeout
  • setImmediate
  • clearImmediate
  • setInterval
  • clearInterval
  • Date

clock.tick(duration)

Forwards the clock duration milliseconds.

clock.restore()

Restores replaced methods manually.

NOTE: In most cases, you don't need to use this method. Please use td.reset() like the usage as written above.

Development

Run test

npm test

Acknowledgment

The API and arguments handling are written based on Sinon.JS.

Changelog

See the Releases page on GitHub.

License

MIT

Author

Yuki Kodama / @kuy

You can’t perform that action at this time.