atlas-cleanup-tests
Version:
Seamlessly wrap sync mocha tests with your cleanup script. Supports 'done' callback.
22 lines (21 loc) • 441 B
JavaScript
const run = (test, cleanup, done) => {
let error, isDone = false;
try {
test(err => {
isDone = true, cleanup(), done(err);
})
}
catch (err) { error = err }
finally {
if (!isDone){
cleanup();
if (error) throw error;
}
}
}
module.exports = cleanup => test => {
// returns a mocha test
return test.length ?
function(done){run(test, cleanup, done)} :
function(){run(test, cleanup)}
}