Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i-spy

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i-spy

Minimal spy library with pass-through

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
increased by425%
Maintainers
2
Weekly downloads
 
Created
Source

i-spy

Build Status

A super simple spying library that mimics the jasmine spy API for use with other frameworks. Doesn't force you into the stub / mock / fake choice like sinon.js.

Installation (nodejs)

  npm install i-spy

Api

createSpy()

Returns a function spy, optionally takes a function that will be executed when the spy is called.

var iSpy = require('i-spy');
var spy = iSpy.createSpy();
var spyWithAction(function () {
  console.log('hi');
});

spy();
spyWithAction(); //-> hi

spy.calls

A 2 dimentional array of all the recorded calls to the spy.

var spy = iSpy.createSpy();

spy('hello', 'world');
spy({foo: 'bar'});

spy.calls[0][0] // -> 'hello'
spy.calls[0][1] // -> 'world'
spy.calls[1][0] // -> '{foo: 'bar'}'

spy.wasCalled()

Returns a boolean that determines whether a spy has been called

var spy = iSpy.createSpy();

spy.wasCalled(); // -> false

spy('anything');
spy.wasCalled(); // -> true

spy.reset()

Clears any recorded calls on the spy

var spy = iSpy.createSpy();

spy(1);
spy.calls.length; // -> 1

spy.reset();

spy.calls.length; // -> 0

Usage

Simple usage:

   var iSpy = require('i-spy');
   var spy = iSpy.createSpy(function fake() {
     // do stuff
     return 'foo';
   });

   describe('frobnicator', function () {

      it('frobnicates', function () {
         frob.fronicate(spy);
         assert(spy.wasCalled());
         assert(spy.calls.length === 1);
         assert(spy.calls[0].length, 2);
      });

   });

Asynchronous tests:

   var iSpy = require('i-spy');

   describe('frobnicator', function () {

      it('frobnicates', function (done) {
        var spy = iSpy.createSpy(function fake(err, frobs) {
          // do stuff with frobs
          done(err);
        });

        frob.fronicate(spy);

        assert(spy.wasCalled());
        assert(spy.calls.length === 1);
        assert(spy.calls[0].length, 2);
        // Assert on err and frobs which are available through spy.calls
      });

   });

Spy behaviour can also be controlled via a fluent interface:

   var iSpy = require('i-spy');

   var erroringSpy = iSpy.createSpy().thatThrows(new Error('Oops'));

   var returningSpy = iSpy.createSpy().thatReturns('Woohoo');

   var callThroughSpy = iSpy.createSpy().thatCalls(function () {
       // Do some stuff
   });

Keywords

FAQs

Package last updated on 13 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc