Skip to content

Instantly share code, notes, and snippets.

@alonn24
Last active August 14, 2018 18:34
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 alonn24/e18fe297fcfd8ad5ca47ce020944d2d9 to your computer and use it in GitHub Desktop.
Save alonn24/e18fe297fcfd8ad5ca47ce020944d2d9 to your computer and use it in GitHub Desktop.
wrap mocha describe and it with custom before and after to reduce testing boilerplate
import {noop} from 'lodash'
const describeWrapper = (context, describe, before = noop, after = noop) => (...args) => {
const [_, impl, options] = args
args[1] = function(...descArgs) {
before.call(this, context, options)
impl.apply(this, [...descArgs, context])
after.call(this, context, options)
}
return describe(...args)
}
const itWrapper = (context, it, before = noop, after = noop) => (...args) => {
const [_, impl] = args
args[1] = async function(...itArgs) {
before.call(this)
await impl.apply(this, [...itArgs, context.testKit])
after.call(this)
}
return it(...args)
}
export default (conf: any = {}) => {
let context = {}
const wDescribe: any = describeWrapper(context, describe, conf.beforeDesc, conf.afterDesc)
wDescribe.only = describeWrapper(context, describe.only, conf.beforeDesc, conf.afterDesc)
wDescribe.skip = describeWrapper(context, describe.skip, conf.beforeDesc, conf.afterDesc)
const wIt: any = itWrapper(context, it, conf.beforeIt, conf.afterIt)
wIt.only = itWrapper(context, it.only, conf.beforeIt, conf.afterIt)
wIt.skip = itWrapper(context, it.skip, conf.beforeIt, conf.afterIt)
return { describe: wDescribe, it: wIt }
}
import TestKit from './test-kit'
import mochaWrapper from './mocha-wrappers'
const startTestKit = async (context, options) => {
context.testKit = new TestKit()
await testKit.start()
}
const cleanTestKit = async context => {
await context.testKit.clean()
context.testKit = null
})
export default () => mochaWrapper({
beforeDesc(context, options) {
this.timeout(3 * 60 * 1000)
before(async () => await startTestKit(context, options))
after(async () => await cleanTestKit(context))
}
})
import MySuit from './suit-wrapper-testkit'
const mySuit = MySuit()
const options = {}
mySuit.describe('my suit', (context) => {
beforeEach(() => {
// do stuff with context before each test
})
mySuit.it('my test', (testKit) => {
// do stuff with testKit
})
}, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment