npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timer-shim

v0.3.0

Published

Test-friendly timer function replacements.

Downloads

16,724

Readme

TIMER-SHIM

All problems in computer science can be solved by another level of indirection

TIMER-SHIM is a simple wrapper around standard timer functions which allows you to stub / test / fake their call and timing function without resorting to tricky global setTimeout/setInterval override.

If you have trouble getting mocha and sinon fake timers to behave, you might find this simple module useful.

Additionally, TIMER-SHIM also provides a few niceties over standard timer functions, including:

  • Call with timeout number before the function.
  • Shorter and simpler aliases without any magic or prototype infection.
  • Validates against NaN and non-function values to save you debugging time.

At its core, it simply delegates the calls to setTimeout/setInterval internally but by calling those function via TIMER-SHIM you can more easily test your time-dependent code.

INSTALL

$ npm install timer-shim --save

USE

var timer = require('timer-shim')
  , count = 0
  , handle = null;

timer.timeout(50, function() { console.log('hello!'); });

handle = timer.interval(100, function() {
  console.log(count++);
  if (count === 10) timer.clear(handle);
});

SAMPLE MOCHA/SINON-CHAI TEST STUBBING

Write your code using the timer module like this:

var timer = require('timer-shim');

function testWithTimeout() {
  // complicated timeout code
  internalState = false
  timer.timeout(100, function() {
    // more complicated code
    internalState = true
  });
}

Then write your test like this:

var timer = require('timer-shim');

describe('timer functionality', function() {
  it('should works', function() {
    sinon.stub(timer, 'timeout').callsArg(1)

    testWithTimeout();
    timer.timeout.should.have.been.calledWith(100);
    interalState.should.be.true

    timer.timeout.restore();
  });
});

API

timer.c
timer.ct
timer.cto
timer.clear
timer.clearTimeout
Clears the timeout handle given.

timer.t
timer.to
timer.timeout
timer.setTimeout
Sets a function to run after the specified timeout

timer.i
timer.in
timer.iv
timer.interval
timer.setInterval
Sets a function to repeatedly every set interval.

Both timer.timeout and timer.interval can be called in either of the following ways:

timer.timeout(100, function() { }); // both works
timer.timeout(function() { }, 100);

timer.interval(100, function() { }); // also works
timer.interval(function() { }, 100);

DEVELOPMENT

Test with:

$ npm install -d && make test

Compiles with:

$ npm install -d && make lib/timer-shim.js

TODOs

  • Ability to fast-forward timers ala sinon fake timers.
  • Ability to infect global setTimeout/setInterval and route it to call the shim functions instead.

LICENSE

BSD

SUPPORT

Just open a GitHub issue or ping me @chakrit on Twitter.