m.spy
v1.0.2
Published
m(icro)spy is a simple test spy implementation written in es6+
Downloads
15
Readme
m.spy
m(icro)spy is a simple test spy implementation written in es6+
What is a test spy?
A test spy is a function that records arguments and thrown exceptions (if any) for all its calls.
Creating a spy as an anonymous function
The spy won’t do anything except record information about its calls. A common use case for this type of spy is testing how a function handles a callback:
const {spy} = require('m.spy')
test('calls listeners on event', () => {
const callback = spy()
const emitter = new EventEmitter()
emitter.on('event', callback)
emitter.emit('event')
assertTrue(callback.called)
})
spy.called
Is true
if the spy was called at least once.
spy.notCalled
Is true
if the spy was not called.
spy.callCount
The number of recorded calls.
spy.args
Array of arguments received, spy.args[0] is an array of arguments received in the first call.
spy.calledWith(arg1[, arg2[, ...]])
Returns true
if spy was called at least once with the provided arguments. Can be used for partial matching, only provided arguments are checked against the actual arguments.