Skip to content

Instantly share code, notes, and snippets.

@jethrolarson
Last active December 10, 2016 01:19
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 jethrolarson/1990c901f5c2540e503c237fbc17b0be to your computer and use it in GitHub Desktop.
Save jethrolarson/1990c901f5c2540e503c237fbc17b0be to your computer and use it in GitHub Desktop.
An alternative to using beforeEach and afterEach
const I = a => a
// this is just wrapping before and after functions around chai's BDD `it`
export const wrapIt = (before = I, after = I) => (label, fn) => {
const ctx = before();
it(label, (ctx) => fn.call(ctx));
after(ctx);
};
const {wrapIt} from 'test.util.js'
describe('thing', () => {
const createThing = () => new Thingy();
const destroyThing = it => it.destroy();
const thing = wrapIt(createThing, destroyThing);
thing('works', me => expect(me).to.be.ok);
thing('uses less boilerplate than last idea', me => expect(me).to.be.ok);
});
describe('stuff', () => {
const createStuff = () => ({
thing: new Thingy(),
nothaThing: new AnotherThing()
});
const destroyStuff = ({thing}) => thing.destroy();
const stuff = wrapIt(createStuff, destroyStuff);
stuff('can test things on the context', function(){
expect(this.thing).to.be.ok;
});
stuff('can still use arrows by destructuring', ({nothaThing}) => expect(nothaThing).to.be.ok);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment