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.
Install (testdouble is a peer dependency):
$ yarn install --dev testdouble testdouble-only-when
Import:
import td from 'testdouble'
import { onlyWhen } from 'testdouble-only-when'Use:
const stub = td.function()
onlyWhen(stub(0)).thenReturn(true)
stub(0) // => true
stub()
// => 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 `()`.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)
You can pass options like with td.when:
const stub = td.function()
onlyWhen(stub(0), { ignoreExtraArgs: true }).thenReturn(true)
stub(0, 0) // => true
stub(0, 1) // => true
stub(1) // => Errortd.when(stub(0)).thenReturn(true)
td.when(stub(1)).thenReturn(false)
failOnOtherCalls(stub)