
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
creating spies for your functions
const spy = require('fn-spy')
spy(function[, thisArg])
function the function to be spied thisArg the this argument for the call (to be used with Classes or Objects)
spy.calledCount() integer, returns the number of times a function as called
spy.calledWith() array, returns the arguments passed to the function
spy.getReturns() array, returns the output of the executed functions
spy.restore() function, restores the function to the initial state
const spyMyEmitter = spy.emitter() and will return the same methods as above
const spyMyEmitter = spy.callback() and will return the same methods as above
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 [[]]
Class / Object example
function BladeRunner () {
this.sn = '9-9-0-6-9-4-7-X-B-7-1'
}
BladeRunner.prototype.getReplicant = function getReplicant () {
return this.sn
}
const deckard = new BladeRunner()
const spy = fnSpy(deckard.getReplicant, deckard)
spy()// returns '9-9-0-6-9-4-7-X-B-7-1'
spy.calledCount()// will return 1
spy.calledWith()// will return [[]]
spy.getReturns()// will return ['9-9-0-6-9-4-7-X-B-7-1']
FAQs
creating spies for your functions
We found that fn-spy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.