testdouble-only-when
Rehearse strict behavior with testdouble.js.
Almost a drop-in replacement for td.when
.
Read this for a thorough motivation of stricter stubs and this helper.
Usage
Install (testdouble is a peer dependency):
$ yarn install --dev testdouble testdouble-only-when
Import:
Use:
const stub = td // => true // => Error:// You invoked a test double in an unexpected fashion.// This test double has 1 stubbings and 2 invocations.// Stubbings:// - when called with `(0)`, then return `true`.// Invocations:// - called with `(0)`.// - called with `()`.
Features
onlyWhen
is almost a drop-in replacement for td.when
. To my knowledge, the only exception are multiple stubbings.
- All stubbing behaviors are supported:
thenReturn
,thenThrow
,thenResolve
,thenReject
,thenDo
,thenCallback
. - Options, e.g.
ignoreExtraArgs
- One-liner stubbings:
onlyWhen(td.func()(0)).thenReturn(true)
- Argument matchers:
onlyWhen(td.func()(td.matchers.anything())).thenReturn(true)
- Sequential return values:
onlyWhen(td.func()(0)).thenReturn(true, false)
Options
You can pass options like with td.when
:
const stub = td // => true // => true // => Error
Multiple stubbings
tdtd