Socket
Book a DemoInstallSign in
Socket

fn-spy

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fn-spy

creating spies for your functions

Source
npmnpm
Version
2.0.0
Version published
Weekly downloads
2
-97.85%
Maintainers
1
Weekly downloads
 
Created
Source

fn-spy

creating spies for your functions

Build StatusCode Coverage 100%ISC LicenseNodeJS

JavaScript Style Guide

api

const spy = require('fn-spy')

spy.calledCount() integer, returns the number of times a function as called

spy.calledWith() array, returns the arguments passed to the function

spy.restore() function, restores the function to the initial state

spy an emitter function

const spyMyEmitter = spy.emitter() and will return the same methods as above

spy a callback function

const spyMyEmitter = spy.callback() and will return the same methods as above

examples

generic example

const spy = require('fn-spy')

function foo (...args) {
  return args.length
}

let spiedFn = spy(foo)

// calledCount
spiedFn()
spiedFn()
spiedFn.calledCount()// will return 2

// calledWith
spiedFn(1, 2, 3)
spiedFn.calledWith()// will return [[1, 2, 3]]

// restore
spiedFn = spiedFn.restore()// now spiedFn is retored to the initial state

emitter example

const EE = require('events')
const spy = require('fn-spy')

const myEmitter = new EE()
const spyEmitter = spy.emitter()

myEmitter.on('hey', spyEmitter)

myEmitter.emit('hey')

spyEmitter.calledCount()// will return 1
spyEmitter.calledWith()// will return [[]]

callback example

const spy = require('fn-spy')

const spyCallback = spy.callback()

 function someFunction (cb) {
    cb()
  }

someFunction(spyCallback)

spyCallback.calledCount()// will return 1
spyCallback.calledWith()// will return [[]]

ISC License (ISC)

Keywords

utils

FAQs

Package last updated on 08 Feb 2017

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