test-fns
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

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()
    })
  })
})

/test-fns/

    Package Sidebar

    Install

    npm i test-fns

    Weekly Downloads

    82

    Version

    1.5.0

    License

    MIT

    Unpacked Size

    29.3 kB

    Total Files

    18

    Last publish

    Collaborators

    • uladkasach