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

doubler

v0.6.0

Published

Basic test doubles for JavaScript testing

Downloads

26

Readme

Doubler

Simple test doubles and stubs for NodeJS testing purposes, designed for usage when tools like Sinon offer more power than your project needs.

npm install --save-dev doubler

Documentation

Until I am able to write up proper docs, the best place to look is the test file, which shows the API nicely.

Doubles

Creating an object with methods on it

Useful when you need to pass an object in that contains multiple methods:

var Double = require('doubler').Double;
var double = new Double({
  // either give it a function to call
  foo: function() { return 2 },
  // or just a single value
  bar: 3
});
double.foo(); //=> 2
double.bar(); //=> 3
double.foo.called; //=> true
double.foo.callCount; //=> 1
double.foo('bar');
double.foo.args; //=> [[], ['bar']]

// reset the double, as if it had never been called
double.foo.reset(); //=> resets double back to new state
double.foo.called; //=> false

Single function double

Often you don't want to create an object with doubles on it but generate just a function:

var doubleFn = Double.function(2);
doubleFn(); //=> 2
doubleFn.called; //=> true
doubleFn.callCount; //=> 1
doubleFn.args; //=> [[]]

You can also give a custom function to call rather than a single return:

var doubleFn = Double.function(function() {
  return 3;
});
doubleFn(); //=> 3
doubleFn.called; //=> true
doubleFn.callCount; //=> 1
doubleFn.args; //=> [[]]

Stubs

var Stub = require('doubler').Stub;

// some object with a method we'd like to stub:
var Foo = {
  get: function() { return 2; }
}

new Stub(Foo, 'get').returnValue(4);
Foo.get(); //=> 4
Foo.get(); //=> 2 (Spies only last for one invocation by default)

new Stub(Foo, 'get').callFunction(function() {
  return 5;
});
Foo.get(); //=> 5

// call persist() to make a stub last forever
var stub = new Stub(Foo, 'get').persist().returnValue(4);
Foo.get(); //=> 4
Foo.get(); //=> 4
Foo.get(); //=> 4 and so on

// entirely remove the stub
stub.destroy();
Foo.get(); //=> 2

Under the hoods stubs actually use doubles, so all the properties on a double that you can use to check invocations are also available on stubs:

var Stub = require('doubler').Stub;

// some object with a method we'd like to stub:
var Foo = {
  get: function() { return 2; }
}

new Stub(Foo, 'get').returnValue(4);
Foo.get(); //=> 4
Foo.get.called; //=> true
Foo.get.callCount; //=> 1
Foo.get.args; //=> [[]]

Changelog

V0.6.0

  • provide Double.function for creating standalone function doubles

V0.5.0

  • Proxy properties from a Stub's underlying double through. You can now call args, callCount and called on a stub.

V0.4.0

  • add #reset method to doubles to allow them to be set back to blank

V0.3.0

  • doubles keep track of their arguments

V0.2.1

  • pass arguments through to stubbed functions

V0.2.0

  • added basic Stubs

V0.1.0

  • initial release