Nebulous Pumpkin Moons

    spyny

    0.2.0 • Public • Published

    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

    Install

    npm i spyny

    DownloadsWeekly Downloads

    0

    Version

    0.2.0

    License

    ISC

    Last publish

    Collaborators

    • callmehiphop