jestdouble
jestdouble is an alternative mock/spy for jest.
- Installation
- Why
- Quick start
- Mocking results
- Conditionally mocking results
- Verifying calls
- Conditionally verifying calls
- Matching
- Notes
- Credit
Installation
$ npm install --save-dev jestdouble
Why
I wanted a mock/spy function that:
- could conditionally mock results / verify calls
- had smart value matching enabled by default
- was compatible with jasmine assertions
- had an awesome API
Quick start
All references to expect
are jest.expect.
const jd = ; const func = ; // create a jest double func;func; ; // 1; // 2; // 2 ;
Mocking results
Invoking a function
{ return num1 + num2;}const mockedSum = ;; // 3
const func = ;func);; // 1
Returning values
returning value
const func = ;func;; // 1
returning this
const object = func: ;objectfunc;object; // object
returning rejected error
const func = ;func;;
returning resolved value
const func = ;func;;
throwing value
const func = ;func;; // throws Error<nope>
calling back error
const func = ;func;;
calling back value
const func = ;func;;
Conditionally mocking results
calledWith
const func = ;func;; // undefined; // 1; // undefined
calledStartingWith
const func = ;func;; // undefined; // 1; // 1
Mocking options
times
const func = ;func;func;func; ; // 1; // 2; // 3; // 3
Verifying calls
jestdouble
is compatible with jasmine assertions.
const func = ;;;;;;;
Conditionally verifying calls
with
const func = ;;not;;
startingWith
const func = ;;;;
Matching
Both mocking results and verifying calls support smart value matching.
const func = ;func;func;; // 1; // 2
const func = ;;;;
Check the API of matchr to learn all the possibilities.
Matching options
There are three matching options.
const td = ; td; const func = ; func; ; // 'OK'
setMatchingOptions
delegates to matchr.setDefaultOptions
.
API
Mock
jestdouble.invokes( arg:function [, options:object] )
jestdouble.returns( arg:* [, options:object] )
jestdouble.returnsThis( [options:object] )
jestdouble.resolves( arg:* [, options:object] )
jestdouble.rejects( arg:* [, options:object] )
jestdouble.callsback( arg:* [, options:object] ) // aliased as callbacks
jestdouble.throws( arg:* [, options:object] )
Conditional mock
jestdouble.calledWith( ...args:* ).invokes( arg:function [, options:object] )
jestdouble.calledWith( ...args:* ).returns( arg:* [, options:object] )
jestdouble.calledWith( ...args:* ).returnsThis( [options:object] )
jestdouble.calledWith( ...args:* ).resolves( arg:* [, options:object] )
jestdouble.calledWith( ...args:* ).rejects( arg:* [, options:object] )
jestdouble.calledWith( ...args:* ).callsback( arg:* [, options:object] ) // aliased as callbacks
jestdouble.calledWith( ...args:* ).throws( arg:* [, options:object] )
jestdouble.calledStartingWith( ...args:* ).invokes( arg:function [, options:object] )
jestdouble.calledStartingWith( ...args:* ) .returns( arg:* [, options:object] )
jestdouble.calledStartingWith( ...args:* ) .returnsThis( [options:object] )
jestdouble.calledStartingWith( ...args:* ) .resolves( arg:* [, options:object] )
jestdouble.calledStartingWith( ...args:* ) .rejects( arg:* [, options:object] )
jestdouble.calledStartingWith( ...args:* ) .callsback( arg:* [, options:object] ) // aliased as callbacks
jestdouble.calledStartingWith( ...args:* ) .throws( arg:* [, options:object] )
Mock options
- times mock result N times
Verify
expect( jestdouble ).toHaveBeenCalled();
expect( jestdouble.with( ...args:* ) ).toHaveBeenCalled();
expect( jestdouble.startingWith( ...args:* ) ).toHaveBeenCalled();
Notes
Results order
Conditionally mocked results will always be returned in favour of mocked results without conditional arguments.
const func = ;func;func;; // Infinity; // 1
const func = ;func;; // 1; // 2
Credit
The name is inspired by jest and testdouble.js. API design is inspired by testdouble.js.