Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Created October 18, 2012 14:49
Show Gist options
  • Save nhunzaker/3912335 to your computer and use it in GitHub Desktop.
Save nhunzaker/3912335 to your computer and use it in GitHub Desktop.
A simple PhantomJS v1.6+ Mocha Reporter
(function() {
var color = Mocha.reporters.Base.color;
function log() {
var args = Array.apply(null, arguments);
if (window.callPhantom) {
window.callPhantom({ message: args.join(" ") });
} else {
console.log( args.join(" ") );
}
}
var Reporter = function(runner){
Mocha.reporters.Base.call(this, runner);
var out = [];
runner.on('start', function() {
out.push([ "Testing", window.location.href, "\n"]);
});
runner.on('suite', function(suite) {
out.push([suite.title]);
});
runner.on("pass", function(test) {
if ('fast' == test.speed) {
out.push([ color('checkmark', ' ✓ '), test.title, "\n" ]);
} else {
out.push([
color('checkmark', ' ✓ '),
test.title,
color(test.speed, test.duration + "ms"),
'\n'
]);
}
});
runner.on('fail', function(test, err) {
out.push([ color('fail', ' × '), color('fail', test.title), ":\n ", err ,"\n"]);
});
runner.on("end", function() {
while (out.length) {
log.apply(null, out.shift());
}
if (window.callPhantom) {
window.callPhantom({ exit: true });
}
});
};
mocha.setup({
ui: 'bdd',
ignoreLeaks: true,
reporter: Reporter
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment