bahmutov / snap-shot

Jest-like snapshot feature for the rest of us, works magically by finding the right caller function
169 stars 3 forks source link

Support data-driven testing ala sazerac #38

Closed bahmutov closed 7 years ago

bahmutov commented 7 years ago

See https://github.com/bahmutov/snap-shot-jest-test#data-driven-testing and https://hackernoon.com/sazerac-data-driven-testing-for-javascript-e3408ac29d8c#.3qht1jpdi and https://github.com/mikec/sazerac

import snapshot from 'snap-shot'
function snap(fn, inputs) {
  const output = inputs.map(fn)
  snapshot(output)
}

it('works for primes', () => {
  snap(isPrime, [2, 3, 5, 7])
})

it('works for non-primes', () => {
  snap(isPrime, [4, 6, 8, 10])
})

Good rule is two find two arguments, first being a function and a list of inputs second.

bahmutov commented 7 years ago

How to support multiple arguments, like this?

function sum(a, b) { return a + b }
it('adds numbers', () => {
  snap(sum, [[1, 2], [2, -4], [2, 3]])
})
// maybe spread the arguments?
function sum(a, b) { return a + b }
it('adds numbers', () => {
  snap(sum, [1, 2], [2, -4], [2, 3])
})
// and for unary function
it('finds primes', () => {
  snap(isPrime, 1, 2, 3, 4, 5, 6)
})
bahmutov commented 7 years ago

How to describe multiple properties, like the multiple chained assertions? From https://github.com/mikec/sazerac-example/blob/master/todos/src/reducers/todos.spec.js

given([], {
    type: 'ADD_TODO',
    text: 'Run the tests',
    id: 0
  })
  .describe('when state is empty and a todo is added')
  .assert('should add the todo and set `id` property', (state) => {
    expect(state[0].id).toEqual(0)
  })
  .assert('should add the todo and set `text` property', (state) => {
    expect(state[0].text).toEqual('Run the tests')
  })
  .assert('should set completed:false on the added todo', (state) => {
    expect(state[0].completed).toEqual(false)
  })
  .assert('should add only one todo', (state) => {
    expect(state.length).toEqual(1)
  })

this could be just single result object with evolved properties.

bahmutov commented 7 years ago

Also good link https://booker.codes/data-driven-tests-in-javascript-using-mocha/

bahmutov commented 7 years ago

Initial experiments in https://github.com/bahmutov/snap-shot-jest-test/commit/19a264e94be571c500a13498aa6c1df1b89c1b7c