Skip to content

segmentio/spy

Repository files navigation

spy

Note
Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use.

Test spy.

CircleCI Codecov

Installation

$ npm install @segment/spy

API

spy(obj, method)

Create a spy with optional obj, method.

Examples:

  var s = spy();
  var s = spy(console, 'log');
  var s = spy(spy);

args[]

An array that holds all arguments.

Examples:

spy(1, 2, 3);
spy(4, 5, 6);

spy.args[0]; // => [1, 2, 3]
spy.args[1]; // => [4, 5, 6]

returns[]

An array that holds all returned values.

Examples:

spy = require('spy')(window.btoa);
spy('test');
spy('foo');

s.returns[0]; // => "dGVzdA=="
s.returns[1]; // => "Zm9v"

.once(), .calledOnce

true if the spy was called only once.

Examples:

spy();
spy.once(); // => true
spy.calledOnce; // => true

spy();
spy.once(); // => false
spy.calledOnce; // => false

.twice(), .calledTwice

true if called twice.

.thrice(), .calledThrice

true if called thrice.

.got(...), .calledWith(...)

true if the spy was called with ...

Examples:

spy(1, 2, 3);
spy.got(1, 2, 3); // => true

spy(4, 5, 6);
spy.got(1, 2, 3); // => false

spy(1, 2, 3);
spy.got(1, 2) // => true

spy(1, 2, 3);
spy.got(1) // => true

.gotExactly(...), .calledWithExactly(...)

true if the spay was called with exactly ...

Examples:

spy(1, 2, 3);
spy.got(1, 2, 3); // => true

spy(4, 5, 6);
spy.got(1, 2, 3); // => false

spy(1, 2, 3);
spy.got(1, 2) // => false

spy(1, 2, 3);
spy.got(1) // => false

.returned(...)

true if the spy was called with ...

Examples:

spy = spy(window.btoa);

spy('test');
spy.returned('dGVzdA=='); // => true