Skip to content

Instantly share code, notes, and snippets.

@crishushu
Last active September 14, 2017 21:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crishushu/fda1efac7badf9b27a3b to your computer and use it in GitHub Desktop.
Save crishushu/fda1efac7badf9b27a3b to your computer and use it in GitHub Desktop.
MochaJS: Mocha controller for Node
var Mocha = require("mocha");
var mocha = new Mocha();
var _suites = [];
var _done = false;
/**
* default mocha options
* for other configurations, check out bin/_mocha
*/
mocha.reporter("nyan");
mocha.useColors(true);
mocha.growl();
module.exports = {
/**
* set interface (bdd is default) and make it global to node
* @param {string} interface bdd|tdd|exports|qunit
*/
init: function(interface) {
interface && mocha.ui(interface);
mocha.suite.emit('pre-require', global, undefined, mocha);
},
/**
* add suite
* @param {function} suite to be executed later
*/
add: function(suite) {
mocha.suite && _suites.push(suite) && suite();
},
/**
* run added suites
*/
run: function(cb) {
console.info('run mocha');
var done = function () {
_done = true;
cb && cb();
process.on('exit', function() {
console.info("exit mocha");
process.exit(1);
});
};
mocha.run(done);
},
/**
* end mocha test
*/
exit: function() {
if (_done) {
process.exit();
} else {
var start = new Date().getTime(),
interval = setInterval(function() {
if (_done) {
console.log("test suites finished");
clearInterval(interval);
process.exit();
} else if (new Date().getTime() - start > 5000) {
console.log("wait for nothing!");
clearInterval(interval);
process.exit();
}
}, 250);
}
},
/**
* change mocha options at runtime, e.g., "reporter", "nyan"
*/
_set: function(key, val){
mocha[property](val);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment