Austin
Add the Powers of spying to JavaScript tests
Install
npm install --save-dev austin
Usage
var austin = require('austin');
var testSubject = {
testFunction: function () {
return 5;
},
anotherFunction: function () {
return 'hello';
}
};
austin.spy(testSubject, 'testFunction');
testSubject.testFunction.callCount();
testSubject.testFunction('green', 3, {x: ['seven']});
testSubject.testFunction.callCount();
testSubject.testFunction.calls[0];
testSubject.testFunction.calledWith('green', 3, {x: ['seven']});
testSubject.testFunction.calledWith('blue');
testSubject.test.returns('a fake value');
testSubject.test();
testSubject.test.throws(TypeError, 'Expected value to be a string');
testSubject.test();
testSubject.test.withArgs('austin').returns('powers');
testSubject.test('austin');
testSubject.test.withArgs('austin').callCount();
testSubject.test.returns('Groovy!')
.withArgs('austin').returns('powers')
.withArgs('oooo').returns('behave');
testSubject.test();
testSubject.test('austin');
testSubject.test('oooo');
testSubject.test('Dr. Evil');
austin.spy(testSubject, 'anotherFunction').returns('bye')
.withArgs('live').returns('dangerously');
testSubject.anotherFunction();
testSubject.anotherFunction('live');
testSubject.testFunction.withArgs('Dr.').returns('Evil');
testSubject.testFunction.reset();
testSubject.testFunction.callCount();
testSubject.testFunction.calls;
testSubject.testFunction('Dr.');
testSubject.testFunction.restore();
testSubject.testFunction.callCount;
testSubject.testFunction();
var nigelPowers = austin.spy();
nigelPowers.returns('Judo Chop');
nigelPowers();
API
austin.spy()
Creates a new Spied Function.
austin.spy(obj, methodName)
Adds spy related utilities to obj[methodName] by transforming the function to a Spied Function.
Note: returns Spied Function for easy chaining with returns and withArgs.
obj
Type: Object
An object that has the desired method methodName
to be spied on.
methodName
Type: *
Can be any type as long as obj[methodName]
is a function
.
Spied Function
A Spied Function has additional properties and methods to aid testing
spiedFunction()
Executes the original function, while updating spy analytics such as call count.
spiedFunction.callCount()
The number of times spiedFunction was executed.
spiedFunction.calledWith(params)
Returns a Boolean
if spiedFunction was called with params
.
params
Type: ...*
Params is a list of any types.
spiedFunction.calls
An array of parameters passed to each execution of Spied Function.
spiedFunction.reset()
Resets spiedFunction.callCount() to 0, spiedFunction.calls to [], and resets any fake values set with returns.
spiedFunction.restore()
Restores the spiedFunction to its original function and removes all spy utilities.
spiedFunction.returns(value)
When spiedFunction is called, it will return value
instead of executing the original function.
Note: returns spiedFunction for easy chaining with withArgs.
Note: if calling spiedFunction.withArgs(...).returns(...)
with params
that already have a fake value, then the
new value passed to returns
will override the previous value.
value
Type: *
Any value that is desired to be returned by spiedFunction().
spiedFunction.throws(errorType[, message])
When spiedFunction is called, it will throw an error instance of errorType
with an optional message message
.
Note: returns spiedFunction for easy chaining with withArgs.
Note: spiedFunction.throws
has priority over spiedFunction.returns
regardless of order called.
errorType
Type: Error
An instance of Error to be thrown.
message
Type: String
An optional message to throw error with.
spiedFunction.withArgs(params)
Returns an object with a returns method to fake return values of calls to spiedFunction with params and callCount and a throws to throw errors when spiedFunction is called with params.
Note: spiedFunction.withArgs(...).returns(...)
and spiedFunction.withArgs(...).throws(...)
return spiedFunction for easy chaining like spiedFunction.withArgs(...).returns(...).withArgs(...).returns(...)
.
params
Type: ...*
An array of any types
LICENSE
MIT © Dustin Specker