npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hanbi

v1.0.3

Published

A small javascript library for stubbing and spying on methods/functions.

Downloads

1,198

Readme

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);
}