Last active
December 10, 2016 01:19
-
-
Save jethrolarson/1990c901f5c2540e503c237fbc17b0be to your computer and use it in GitHub Desktop.
An alternative to using beforeEach and afterEach
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 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); | |
}; | |
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 {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