Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

deepsweet/spyfn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spyfn

⚠️ Project has been transferred to NexTools metarepo

npm tests coverage

Spy function.

Requirements

  • Node.js >= 8.6.0

Install

$ yarn add --dev spyfn

Usage

import { createSpy, getSpyCalls } from 'spyfn'

const spy = createSpy(({ index, args }) => {
  switch (index) {
    case 0: {
      return `first call result, args: ${args}`
    }
    case 1: {
      return `second call result, args: ${args}`
    }
    default: {
      return `3+ call result, args: ${args}`
    }
  }
})

console.log(spy('foo')) // first call result, args: ['foo']
console.log(spy('bar')) // second call result, args: ['bar']
console.log(getSpyCalls(spy)) // [['foo'], ['bar']]

console.log(spy('baz')) // 3+ call result, args: ['baz']
console.log(spy('qux')) // 3+ call result, args: ['qux']
console.log(getSpyCalls(spy)) // [['foo'], ['bar'], ['baz'], ['qux']]