Created
May 15, 2019 22:03
-
-
Save JonDotsoy/40b74dc9f0a8ef8bb91d3a3790c01398 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface Options { | |
start: (this: Mocha) => any; | |
run?: (this: Mocha) => any; | |
end?: (this: Mocha) => any; | |
} | |
export function preMocha({ start, run, end }: Options) { | |
const mocha = require('mocha/lib/mocha'); | |
const originalRun = mocha.prototype.run; | |
mocha.prototype.run = async function preRun(this: Mocha, ...args: any[]) { | |
try { | |
await start.apply(this); | |
run && this.suite.beforeAll(() => run.apply(this)); | |
end && this.suite.afterAll(() => end.apply(this)); | |
const returns = originalRun.apply(this, args); | |
return returns; | |
} catch (error) { | |
console.error(error.stack || error); | |
process.exit(1); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment