Created
October 27, 2018 16:08
-
-
Save PCreations/138fa7f20b2713b5c20f0461a3e9173b to your computer and use it in GitHub Desktop.
LIttle helpers methods to help you write unit test with jest in a more "behavior oriented" way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const given = (name, cb = () => {}) => [cb, describe.bind(describe, `given ${name}`)]; | |
const and = (name, cb = () => {}) => [cb, describe.bind(describe, `and ${name}`)]; | |
const when = (name, cb = () => {}) => [cb, describe.bind(describe, `when ${name}`)]; | |
const then = (name, cb = () => {}) => [cb, test.bind(test, `then ${name}`)]; | |
const feature = (name, getSteps) => { | |
const steps = [[() => {}, describe.bind(describe, 'Feature: basic synchronous command')], ...getSteps()]; | |
const lastStep = steps.pop(); | |
const [cb, step] = steps.reduceRight(([combinedCb, combinedStep], currentFn) => { | |
const [cb, step] = currentFn; | |
return [ | |
(...args) => { | |
cb(...args); | |
combinedStep(combinedCb); | |
}, | |
step, | |
]; | |
}, lastStep); | |
step(cb); | |
}; | |
feature('some feature to test', () => { | |
/* some state for this feature */ | |
return [ | |
given('some precondition'), | |
and('another precondition'), | |
when('something happens', () => { | |
/* let's something to happen */ | |
}), | |
and('other thing happens', () => { | |
/* let's make another thing to happen */ | |
}), | |
then('the invariants should not be violated', () => { | |
/* test the invariants */; | |
}), | |
]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment