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

spyny

v0.2.0

Published

Tiny spy library

Downloads

2

Readme

spyny

A tiny spy library for Node.js.

spy([callback])

Use the spy function to create a new spy.

var spy = require('spyny');
var mySpy = spy();

You can also provide a callback to be ran when the spy is called. It will recieve all arguments that are passed to the spy.

var mySpy = spy(function(name) {
  return 'Hello, ' + name + '!';
});

console.log(mySpy('Dave'));
// Hello, Dave!

called

Check if a spy was called.

mySpy();
console.log(mySpy.called);
// true

callCount

Check how many times a spy was called.

mySpy();
mySpy();
console.log(mySpy.callCount);
// 2

callWith()

Alternative way of setting the spy callback.

mySpy.callWith(function(name) {
  return 'Yo, ' + name + '!';
});

console.log(mySpy('Dave'));
// Yo, Dave!

getCall()

Get the details of a specific call.

mySpy('yo!');
console.log(mySpy.getCall(0));
// {
//   args: ['yo!']
// }

reset()

Flush all previous call data.

mySpy();
mySpy.reset();
console.log(mySpy.called);
// false

returns()

Assign a return value for the spy.

mySpy.returns('Howdy!');
console.log(mySpy());
// Howdy!

===

spy.on(obj, method, [callback])

Spy on an objects method. Contains all properties and methods of spy().

var Dave = {
  greet: function(person) {
    return 'Hello, ' + person + '!';
  }
};

spy.on(Dave, 'greet').returns('Howdy');
console.log(Dave.greet('Gabe'));
// Howdy

You can also pass in a callback to be invoked.

spy.on(Dave, 'greet', function(person) {
  return 'Howdy, ' + person + '.';
});

console.log(Dave.greet('Gabe'));
// Howdy, Gabe.

restore()

Restore a method to its previous state.

spy.on(Dave, 'greet');
Dave.greet.restore();
console.log(Dave.greet('Gabe'));
// Hello, Gabe!

passthrough()

When called it instructs the spy to use the original method as the callback.

spy.on(Dave, 'greet').passthrough();
console.log(Dave.greet('Gabe'));
// Hello, Gabe!
console.log(Dave.greet.getCall(0));
// {
//   args: ['Gabe']
// }

===

spy.sandbox()

Create a sandbox object to contain all of your spies. Useful for mass cleanups.

var sandbox = require('spyny').sandbox();
var spy = sandbox.spy;

spy([callback])

Creates a spy contained within the sandbox. Contains the same properties and methods as a normal spy.

var sandbox = require('spyny').sandbox();
var spy = sandbox.spy;
var mySpy = spy();

mySpy();
console.log(mySpy.called);
// true

spy.on(obj, method, [callback])

Creates a spy for an object's method within the sandbox. Contains the same properties and methods as a normal spy.on()

var sandbox = require('spyny').sandbox();
var spy = sandbox.spy;

spy.on(Dave, 'greet').passthrough();
console.log(Dave.greet.called);
// false

reset()

Resets all spies within the sandbox.

var sandbox = require('spyny').sandbox();
var spy = sandbox.spy;
var spy1 = spy();
var spy2 = spy();

spy1();
spy2();
spy2();

sandbox.reset();

console.log(spy1.called);
// false
console.log(spy2.callCount);
// 0

restore()

Restores all spies created via spy.on() to previous state.

var sandbox = require('spyny').sandbox();
var spy = sandbox.spy;

spy.on(Dave, 'greet', function() {
  return 'asdf!';
});

sandbox.restore();
console.log(Dave.greet('Gabe'));
// Hello, Gabe!

flush()

Clear all cached spies from the sandbox. Note: this does not restore/reset spies, this simply clears them all from the sandbox cache.

var sandbox = require('spyny').sandbox();
var spy = sandbox.spy;

spy.on(Dave, 'greet', function() {
  return 'hi';
});

sandbox.flush();
sandbox.restore(); // `Dave.greet` is still a spy

License

ISC