1.1.0 • Published 6 years ago

testdouble-only-when v1.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

npm version code style: prettier

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:

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 `()`.​​

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.function()
onlyWhen(stub(0), { ignoreExtraArgs: true }).thenReturn(true)

stub(0, 0)  // => true
stub(0, 1)  // => true
stub(1)     // => Error

Multiple stubbings

td.when(stub(0)).thenReturn(true)
td.when(stub(1)).thenReturn(false)
failOnOtherCalls(stub)
1.1.0

6 years ago

1.0.0

6 years ago

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago