Skip to content

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

License

Notifications You must be signed in to change notification settings

ehmpathy/test-fns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published