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

spy

v1.0.0

Published

spy and mock for simple testcase

Downloads

3,060

Readme

spy

NPM version Spm version Build Status Build Status NPM downloads

spy and mock for simple testcase


Why Spy?

Sinon is the best spy, stub, mock lib, I use it in my every repo.

But Sinon is too big, I just use spy and simple mock, and I think it can be separated to different repo.

So I write Spy

  • written in commonjs, support package mananger for browser such as spm, component, bower.
  • support spy and simple mock, see API below

Install

$ npm install spy -g

Install for browser

spm

spm install spy

component

component install popomore/spy

bower

bower install spy

Usage

var spy = require('spy');
var s = spy();
s(1);
s.called // return true
s.callCount // return 1
s.calledWith(1) // return true

mock spy function

var spy = require('spy');
var s = spy();
s.mock(1);
s(); // return 1

mock module

// a.js
module.exports = function() {}

// b.js
var a = require('spy').require('./a.js');
a();
a.callCount // return 1

API

spy()

Create a anonymous Spy function

spy(func)

Create a Spy function wrapped func

spy(obj, 'func')

Create a Spy function wrapped obj.func

Spy

Spy function can be used as a normal function, and it will record what the function do.

By default, Spy will pass arguments to original function, get result from the function, and use the same context;

Otherwise, using spy.mock will call the mock function instead of calling the original function,

spy.obj

The object that contain the refer to the function

spy(obj, 'a'); // obj === spy.obj

spy.method

The original method

spy(obj, 'a'); // obj.a === spy.method
spy(func); // func === spy.method
spy(); // spy.method is an anonymous function

spy.methodName

The method name of the given function

spy(obj, 'a'); // 'a' === spy.method

spy.called

Return true if the spy function has been called.

spy.callCount

Return the count that the spy function has been called.

spy.calls

Contain the call list in order, each call is the Call instance.

spy(); // spy.call[0]
spy(); // spy.call[1]

spy.mock()

Mock the return value of the spy

spy.mock(1);
spy(); // return 1

You can do it just like a normal function

spy.mock(function(arg1, arg2, arg3) {
	// 1. return arg1
	// 2. return this
	// 3. throw new Error();
});

Support generator!!!

spy.mock(function* () {
  var pkg = yield read('package.json');
  return pkg.name;
});

spy.reset()

Reset the call record and the mock method

spy.restore()

Restore the refer to the function

spy.calledBefore / spy.calledAfter

In the call sequence, determine whether one spy is called before/after the other.

spy1();
spy2();
spy1.calledBefore(spy2); // true
spy2.calledAfter(spy1); // true

spy.calledWith / spy.alwaysCalledWith / spy.neverCalledWith

Same as call.calledWith, but will match spy.calls in diffent ways.

  • spy.calledWith, whether one of the calls called with arguments, just like

    calls.some(function(call){
      return call.calledWith()
    });
  • spy.alwaysCalledWith, whether all calls called with arguments, just like

    calls.every(function(call){
      return call.calledWith()
    });
  • spy.neverCalledWith, whether all calls didn't call with arguments, just like

    calls.every(function(call){
      return !call.calledWith()
    });

spy.calledWithNew / spy.alwaysCalledWithNew / spy.neverCalledWithNew

Same as spy.calledWithNew, but will match spy.calls in diffent ways.

The different between always and never see spy.calledWith

spy.calledWithExactly / spy.alwaysCalledWithExactly / spy.neverCalledWithExactly

Same as spy.calledWithExactly, but will match spy.calls in diffent ways.

The different between always and never see spy.calledWith

spy.calledOn / spy.alwaysCalledOn / spy.neverCalledOn

Same as spy.calledOn, but will match spy.calls in diffent ways.

The different between always and never see spy.calledWith

spy.returned / spy.alwaysReturned / spy.neverReturned

Same as spy.returned, but will match spy.calls in diffent ways.

The different between always and never see spy.calledWith

spy.threw / spy.alwaysThrew / spy.neverThrew

Same as spy.threw, but will match spy.calls in diffent ways.

The different between always and never see spy.calledWith

Call

Call object will record every function call.

call.calledWith

Return true if match the call arguments

spy(1, 2, 3)
spy.calls[0].calldWith(1) // true
spy.calls[0].calldWith(1, 2) // true
spy.calls[0].calldWith(1, 2, 3) // true

call.calledWithExactly

Same as calledWith, but assert the arguments length

spy(1, 2, 3)
spy.calls[0].calledWith(1) // false
spy.calls[0].calledWith(1, 2) // false
spy.calls[0].calledWith(1, 2, 3) // true

call.calledWithNew

Return true if it instantiation

new spy()
spy.calls[0].calledWithNew() // true

call.calledOn

Return true if it called with the context

spy.call(ctx)
spy.calls[0].calledOn(ctx) // true

call.returned

Return true if get the matched return

function spiedFunc() { return 1; }
var spy = require('spy')(spiedFunc);
spy()
spy.calls[0].returned(1) // true

call.threw

Return true if the function had threw

function spiedFunc() { throw new Error(); }
var spy = require('spy')(spiedFunc);
spy()
spy.calls[0].threw() // true

LICENSE

Copyright (c) 2014 popomore. Licensed under the MIT license.