Coder Social home page Coder Social logo

observable-calls's Introduction

The main purpose of this library is to provide a mean to observe function calls during unit tests. If it fits any need beyond testing, feel free to report this new usage context to enhance the documentation.

Type npm install observable-calls to get the library, you can then use it this way:

const Observable = require('zen-observable');
const spy = require('observable-calls')(Observable);

const f = spy((a, b) => a + b)
f.calls(3).subscribe({
  next: console.log,
  complete: () => console.log('complete')
});

f(1, 2); // {args: [1, 2], output: 3}
f(2, 3); // {args: [2, 3], output: 5}
f(3, 4); // {args: [3, 4], output: 7}
// complete
f(4, 5); // no more console output

The library was tested with zen-observable but any compliant Observable library should be usable.

The library module exports a function with the following signature: Observable => function => function

The first call specifies the Observable implementation library to use. The second call will transform any function in a new function with a calls property.

This calls property is a function providing a new Observable at each call. It takes an integer as its only argument in order to specify the number of calls to observe until completion.

The values that can be emitted by the Observable are:

  • {args: [...], output: ...} in case of a successful function call;
  • {args: [...], err: ...} in case of an exception raised by a function call.

If calls is called with no argument, the default number of calls to observe is 0 which means that the new Observable will immediately complete. If you want the Observable to never complete, pass Infinity as the calls argument.

observable-calls's People

Contributors

gautaz avatar

Watchers

James Cloos avatar Fabrice Le Coz avatar  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.