
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;
stub(fn)
Creates a wrapped version of a given function which tracks any calls.
const fn = () => 5;
const stub = hanbi.stub(fn);
stub.handler();
stub.called;
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();
stub.called;
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();
restore();
instance.myMethod();
Stub API
Each of the above mentioned entry points returns a Stub which has
several useful methods.
class Stub {
handler;
restoreCallback;
original;
called;
calls;
getCall(index);
firstCall;
lastCall;
callCount;
returns(val);
callsFake(fn);
passThrough();
reset();
restore();
calledWith(...args);
returned(val);
}