m.spy

    1.0.2 • Public • Published

    m.spy

    m(icro)spy is a simple test spy implementation written in es6+

    travis dependencies coverage status linter

    node version license minzip downloads

    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.

    Install

    npm i m.spy

    DownloadsWeekly Downloads

    4

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    4.67 kB

    Total Files

    4

    Last publish

    Collaborators

    • ivoputzer