UNPKG

mocha-annotated

Version:
119 lines (89 loc) 2.76 kB
'use strict'; var Base = require('mocha/lib/reporters/base'); var _require = require('mocha/lib/utils'), inherits = _require.inherits; var ms = require('mocha/lib/ms'); var color = Base.color; /** * Initialize a new `AnnotatedSpec` test reporter. * * @api public * @param {Runner} runner */ function AnnotatedSpec(runner) { var _this = this; Base.call(this, runner); this.indents = 0; this.n = 0; runner.on('start', function () { console.log(); }); runner.on('suite', function (suite) { _this.indents += 1; console.log(color('suite', '%s%s'), _this.indent(), suite.title); }); runner.on('suite end', function () { _this.indents -= 1; if (_this.indents === 1) { console.log(); } }); runner.on('pending', function (test) { var fmt = _this.indent() + color('pending', ' - %s'); console.log(fmt, test.title); }); runner.on('pass', function (test) { var fmt = void 0; if (test.speed === 'fast') { fmt = _this.indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s'); console.log(fmt, test.title); } else { fmt = _this.indent() + color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s') + color(test.speed, ' (%dms)'); console.log(fmt, test.title, test.duration); } }); runner.on('fail', function (test) { _this.n += 1; console.log(_this.indent() + color('fail', ' %d) %s'), _this.n, test.title); }); runner.once('end', function () { var fmt = void 0; // passes fmt = color('bright pass', ' ') + color('green', ' %d passing') + color('light', ' (%s)'); console.log(fmt, _this.stats.passes || 0, ms(_this.stats.duration)); // pending if (_this.stats.pending) { fmt = color('pending', ' ') + color('pending', ' %d pending'); console.log(fmt, _this.stats.pending); } // failures if (_this.stats.failures) { fmt = color('fail', ' %d failing'); console.log(fmt, _this.stats.failures); Base.list(_this.failures); console.log(); // extended info _this.failures.filter(function (failure) { return failure.feedback !== undefined; }).forEach(function (failure, i) { var failureFmt = color('bright fail', ' %d) Task %d: %s'); console.log(failureFmt, i + 1, failure.task, failure.title); console.log(failure.feedback.replace(/^/gm, ' ')); console.log(); }); console.log(); } console.log(); }); } AnnotatedSpec.prototype.indent = function indent() { return [].fill(' ', 0, this.indents); }; /** * Inherit from `Spec.prototype`. */ inherits(AnnotatedSpec, Base); /** * Expose `AnnotatedSpec`. */ module.exports = AnnotatedSpec;