Skip to content

Instantly share code, notes, and snippets.

@JonDotsoy
Created May 15, 2019 22:03
Show Gist options
  • Save JonDotsoy/40b74dc9f0a8ef8bb91d3a3790c01398 to your computer and use it in GitHub Desktop.
Save JonDotsoy/40b74dc9f0a8ef8bb91d3a3790c01398 to your computer and use it in GitHub Desktop.
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