Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

test-fns

ehmpathy519MIT1.5.0TypeScript support: included

write usecase driven tests systematically for simpler, safer, and more readable code

test, usecase, given, when, then, behavior

readme

test-fns

ci_on_commit deploy_on_tag

write usecase driven tests systematically for simpler, safer, and more readable code

purpose

establishes a pattern of writing tests for simpler, safer, and more readable code.

by defining tests in terms of usecases (given, when, then) your tests are

  • simpler to write
  • easier to read
  • safer to trust

install

npm install --save test-fns

use

type Plant = { id: number, hydration: 'DRY' | 'WET' };
const doesPlantNeedWater = (plant: Plant) => plant.hydration === 'DRY';

describe('doesPlantNeedWater', () => {
  given('a plant', () => {
    when('the plant doesnt have enough water', () => {
      const plant: Plant = {
        id: 7,
        hydration: 'DRY',
      };
      then('it should return true', () => {
        expect(doesPlantNeedWater(plant)).toEqual(true)
      })
    })
  })
})

produces

 PASS  src/givenWhenThen.test.ts
  doesPlantNeedWater
    given: a plant
      when: the plant doesnt have enough water
        ✓ then: it should return true (1 ms)

features

.runIf(condition) && .skipIf(condition)

skip running the suite if the condition is not met

describe('your test', () => {
  given.runIf(onLocalMachine)('some test that should only run locally', () => {
    then.skipIf(onProduction)('some test that should not run against production', () => {
      expect(onProduction).toBeFalse()
    })
  })
})

changelog

Changelog

1.5.0 (2024-08-03)

Features

  • uuid: expose genTestUuid procedure (2c17d2c)

1.4.2 (2024-07-07)

Bug Fixes

  • deps: bump error-fns dep for better devex (d952ef0)

1.4.1 (2024-06-16)

Bug Fixes

  • then: ensure .todo only passes name of test (924df7d)

1.4.0 (2024-06-06)

Features

  • retry: expose then.repeatably every and some (b2c56a9)

1.3.0 (2024-02-03)

Features

  • todo: add .todo method onto then (81835a4)

1.2.0 (2024-02-01)

Features

  • if: add .runIf and .skipIf to given,when,then (7fb0890)

1.1.1 (2023-10-15)

Bug Fixes

  • why: specify why of then in less visually large way (0a673c2)

1.1.0 (2023-10-15)

Features

  • why: allow thens to specify what and why explicitly (30952fe)

1.0.3 (2023-09-17)

Bug Fixes

  • pkg: forward the getError method from error-fns for convinience (365b2d2)

1.0.2 (2023-09-16)

Bug Fixes

  • defs: enable .skip and .only (2b1a68a)

1.0.1 (2023-09-16)

Bug Fixes

  • pkg: expose exports via index (9766143)

1.0.0 (2023-09-16)

Features

  • init: initialize based on error-fns (362d38e)