Skip to content

Instantly share code, notes, and snippets.

@apla
Created January 24, 2016 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apla/68fece4fd3db4cbe7691 to your computer and use it in GitHub Desktop.
Save apla/68fece4fd3db4cbe7691 to your computer and use it in GitHub Desktop.
base mocha reporter for browser test
<html>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.3.4/mocha.min.js"></script>
<script type="text/javascript">
function reporter (runner) {
var stats = this.stats = { suites: 0, tests: 0, passes: 0, pending: 0, failures: 0 };
var failures = this.failures = [];
if (!runner) {
return;
}
this.runner = runner;
runner.stats = stats;
runner.on('start', function() {
stats.start = new Date();
});
runner.on('suite', function(suite) {
stats.suites = stats.suites || 0;
suite.root || stats.suites++;
});
runner.on('test end', function() {
stats.tests = stats.tests || 0;
stats.tests++;
});
runner.on('pass', function(test) {
stats.passes = stats.passes || 0;
if (test.duration > test.slow()) {
test.speed = 'slow';
} else if (test.duration > test.slow() / 2) {
test.speed = 'medium';
} else {
test.speed = 'fast';
}
stats.passes++;
});
runner.on('fail', function(test, err) {
stats.failures = stats.failures || 0;
stats.failures++;
test.err = err;
failures.push(test);
});
runner.on('end', function() {
stats.end = new Date();
stats.duration = new Date() - stats.start;
console.log ('tests finished in %s, %o', stats.duration, stats);
});
runner.on('pending', function() {
stats.pending++;
});
}
mocha.setup('bdd');
describe ("should run", function () {
it ("shall pass", function () {
return true;
});
});
mocha.reporter (reporter);
window.onload = function() {
mocha.run()
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment