chain-spy
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

chain-spy Build Status codecov

Lets you spy on a chain

Install

$ npm install chain-spy

Usage

import { create } from 'chain-spy'

function select(db) {
  return db
    .select('id', 'name', 'age')
    .where('name', 'Daenerys')
    .sort('age')
    .someFunction(5)
}


const dbProxy = create({ someFunction: n => n })

const result = select(dbProxy)


console.log(result)
// 5
console.log(dbProxy.__execution_log__)
// [ { type: 'get', name: 'select' },
//   { type: 'apply', args: [ 'id', 'name', 'age' ] },
//   { type: 'get', name: 'where' },
//   { type: 'apply', args: [ 'name', 'Daenerys' ] },
//   { type: 'get', name: 'sort' },
//   { type: 'apply', args: [ 'age' ] } ]
//   { type: 'get', name: 'someFunction' },
//   { type: 'apply', args: [ 5 ] } ]

// Can be used with snapshot testing, eg.
expect(dbProxy.__execution_log__).toMatchSnapshot() // jest
t.snapshot(dbProxy.__execution_log__); // ava

API

create(overwrite?)

overwrite (optional)

Type: object

Can overwrite the return value of a property. eg.

const proxy = create({ hello: 'world', cat: n => n + 1 })

proxy.hello // world
proxy.something.hello // world
proxy.something('else').hello // world
proxy.cat(1) // 2

Can also access the proxy inside a overwrite function:

const proxy = create({ hello: function(n) {
  return n === 5 ? 7 : this
}})

proxy.hello(1).hello(2).hello(5) // 7

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i chain-spy

Weekly Downloads

13

Version

1.0.1

License

MIT

Unpacked Size

6.18 kB

Total Files

11

Last publish

Collaborators

  • tjoskar