All Projects → 43081j → hanbi

43081j / hanbi

Licence: MIT license
A small javascript library for stubbing and spying on methods/functions.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to hanbi

aem-stubs
Tool for providing sample data for AEM applications in a simple and flexible way. Stubbing server on AEM, no separate needed.
Stars: ✭ 40 (-18.37%)
Mutual labels:  stubs, mocks
Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+3259.18%)
Mutual labels:  stubs, mocks
observer-spy
This library makes RxJS Observables testing easy!
Stars: ✭ 310 (+532.65%)
Mutual labels:  mocks, spies
jsdom-testing-mocks
A set of tools for emulating browser behavior in jsdom environment
Stars: ✭ 37 (-24.49%)
Mutual labels:  mocks
Faker.js
generate massive amounts of realistic fake data in Node.js and the browser
Stars: ✭ 34,329 (+69959.18%)
Mutual labels:  mocks
mockk-guidebook
A guide to using the MockK testing library.
Stars: ✭ 37 (-24.49%)
Mutual labels:  mocks
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (-48.98%)
Mutual labels:  mocks
umock-c
A pure C mocking library
Stars: ✭ 29 (-40.82%)
Mutual labels:  mocks
Sinon
Test spies, stubs and mocks for JavaScript.
Stars: ✭ 8,828 (+17916.33%)
Mutual labels:  stubs
woocommerce-stubs
WooCommerce function and class declaration stubs for static analysis.
Stars: ✭ 49 (+0%)
Mutual labels:  stubs
micropy-stubs
Automatically Generated Stub Packages for Micropy-Cli and whomever else
Stars: ✭ 25 (-48.98%)
Mutual labels:  stubs
ruby-api-stubs
Auto-generated stubs for the SketchUp Ruby API. Useful for IDE intellisense and auto-complete.
Stars: ✭ 19 (-61.22%)
Mutual labels:  stubs
Workflow
一个工作流平台
Stars: ✭ 1,888 (+3753.06%)
Mutual labels:  mocks
php-stub-generator
A tool to generate stub-files for your php classes.
Stars: ✭ 26 (-46.94%)
Mutual labels:  stubs
mock-inspect
Mocks network requests and allows you to make assertions about how these requests happened. Supports auto-mocking of graphQL requests given a valid schema.
Stars: ✭ 19 (-61.22%)
Mutual labels:  mocks
node-mock-factory
🐋 node-mock-factory
Stars: ✭ 26 (-46.94%)
Mutual labels:  mocks
Mockery
A mock code autogenerator for Golang
Stars: ✭ 3,138 (+6304.08%)
Mutual labels:  mocks
laravel-repository-pattern
Files autogenerator for repositorry pattern
Stars: ✭ 46 (-6.12%)
Mutual labels:  stubs
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+42.86%)
Mutual labels:  mocks
Unmockable
💉 ↪️ 🎁 Unmockable objects wrapping in .NET
Stars: ✭ 35 (-28.57%)
Mutual labels:  mocks

npm

hanbi

hanbi is a rather small and simple library for stubbing and spying on methods and functions in JavaScript tests.

Install

$ npm i -D hanbi

Usage

spy()

Creates a single "spy" function to be used as input into some other function.

const spy = hanbi.spy();
window.addEventListener('load', spy.handler);
spy.called; // true once the event fires

stub(fn)

Creates a wrapped version of a given function which tracks any calls.

const fn = () => 5;
const stub = hanbi.stub(fn);
stub.handler(); // undefined
stub.called; // true

stubMethod(obj, method)

Replaces a given method on an object with a wrapped (stubbed) version of it.

class Foo {
  myMethod() {
    return 5;
  }
}
const instance = new Foo();
const stub = hanbi.stubMethod(instance, 'myMethod');
instance.myMethod(); // undefined
stub.called; // true

restore()

Restores all stubs/spies to their original functions.

class Foo {
  myMethod() {
    return 5;
  }
}
const instance = new Foo();
const stub = hanbi.stubMethod(instance, 'myMethod');
instance.myMethod(); // undefined
restore();
instance.myMethod(); // 5

Stub API

Each of the above mentioned entry points returns a Stub which has several useful methods.

class Stub {
  /**
   * Wrapped function
   */
  handler;

  /**
   * Function to be called when stub is restored
   */
  restoreCallback;

  /**
   * Original function
   */
  original;

  /**
   * Whether or not this stub has been called
   */
  called;

  /**
   * List of all calls this stub has received
   */
  calls;

  /**
   * Retrieves an individual call
   * @param index Index of the call to retrieve
   * @return Call at the specified index
   */
  getCall(index);

  /**
   * Retrieves the first call
   * @return Call object
   */
  firstCall;

  /**
   * Retrieves the last call
   * @return Call object
   */
  lastCall;

  /**
   * Number of times this stub has been called
   */
  callCount;

  /**
   * Specifies the value this stub should return
   * @param val Value to return
   */
  returns(val);

  /**
   * Specifies a function to call to retrieve the return value of this
   * stub
   * @param fn Function to call
   */
  callsFake(fn);

  /**
   * Enables pass-through, in that the original function is called when
   * this stub is called.
   */
  passThrough();

  /**
   * Resets call state (e.g. call count, calls, etc.)
   */
  reset();

  /**
   * Restores this stub.
   * This behaviour differs depending on what created the stub.
   */
  restore();

  /**
   * Asserts that the stub was called with a set of arguments
   * @param args Arguments to assert for
   * @return Whether they were passed or not
   */
  calledWith(...args);

  /**
   * Asserts that the stub returned a given value
   * @param val Value to check for
   * @return Whether the value was ever returned or not
   */
  returned(val);
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].