# i-spy

> Minimal spy library with pass-through

Latest version **1.0.1** (published 2016-07-13) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install i-spy
pnpm add i-spy
yarn add i-spy
bun add i-spy
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-07-13 |
| First published | 2016-06-14 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | 7digital |
| Maintainers | eddhannay, willm |
| Keywords | spies, mocha, test, spy |

## Links

- npm: https://www.npmjs.com/package/i-spy
- Repository: https://github.com/7digital/i-spy
- Issues: https://github.com/7digital/i-spy/issues
- npm.io page: https://npm.io/package/i-spy

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-07-13
- 1.0.0 — 2016-06-15
- 0.0.4 — 2016-06-15
- 0.0.3 — 2016-06-14

## README

i-spy
=====

[![Build Status](https://travis-ci.org/7digital/i-spy.png?branch=master)](http://travis-ci.org/7digital/i-spy)

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)
---------------------

```bash
  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.

```javascript
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

```javascript
var spy = iSpy.createSpy();

spy.wasCalled(); // -> false

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

`spy.reset()`
-----
Clears any recorded calls on the spy

```javascript
var spy = iSpy.createSpy();

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

spy.reset();

spy.calls.length; // -> 0
```



Usage
-----

Simple usage:

```javascript
   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:

```javascript
   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:

```javascript
   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
   });
```

---
_Source: https://npm.io/package/i-spy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
