Giter VIP home page Giter VIP logo

dark-hole's Introduction

dark-hole

Build Status Coverage

dark-hole

dark-hole swallows(accepts) everything, and everything just disappears(gone). Like a blackhole.

dark-hole is useful if you pretend to implement some interfaces but not actually implement them.

And you could trace everything that swallowed by dark-hole, which is very useful for testing purpose.

Install

$ npm i dark-hole

Usage

const {
  create,
  trace
} = require('dark-hole')

// Create a blackhole which swallows everything
const blackhole = create()

const returnValue = blackhole
.whateverProperty          // 1
.asAccessingArrayItem[0]   // 2, 3
.runFunction('blah blah')  // 3, 4
.destroyTheWorld           // 5
.boooooooom                // 6
.neverDie()                // 7, 8
// Nothing happens. Nothing. Without errors.

Trace accessing and invoking

// Create a tracer which could trace back upon time
// to the very beginning of the `blackhole`
// just like a time machine
const tracer = trace(returnValue)

// `willBeCalledWith()` returns a new tracker afterwards
// if there is a match
const after4 = tracer.willBeCalledWith({
  // accessing the property from any time spot is ok
  accessor: 'asAccessingArrayItem.0.runFunction',
  args: ['blah blah']
})

try {
  tracer.willBeCalledWith({
    accessor: 'asAccessingArrayItem.0.runFunction',
    args: ['blah blah'],
    // The accessing must happens immediately
    immediately: true
  })
} catch (err) {
  console.log(err.code)
  // 'NO_MATCH'
  // It was not accessed immediately.
}

Only trace accessing

const after7 = after4.willBeAccessedBy({
  // `accessor` could be an array
  accessor: ['destroyTheWorld', 'boooooooom', 'neverDie'],
  immediately: true
})

// No errors

Only trace function call

const end = after7.willBeCalledWith({
  args: [],
  immediately: true
})

console.log(end.ended)
// true
// , if nothing happens afterwards

APIs

create() : Proxy

Returns a dark hole

trace(DarkHole) : Tracer

tracer.willBeCalledWith(options): Tracer

  • accessor? string | Array<string>
  • thisArg? any If options does not contain the 'thisArg' property, then it will match any this argument. Or it will do a exact match.
  • args? Array<any>=[]
  • immediately? boolean

Returns Tracer if there is a match.

Or an NO_MATCH error will be thrown.

tracer.willBeAccessedBy(options): Tracer

  • accessor string | Array<string>
  • immediately? boolean

Returns Tracer if there is a match.

Or an NO_MATCH error will be thrown.

License

MIT

dark-hole's People

Contributors

kaelzhang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.