Skip to content

Instantly share code, notes, and snippets.

@PCreations
Created October 27, 2018 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PCreations/138fa7f20b2713b5c20f0461a3e9173b to your computer and use it in GitHub Desktop.
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
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