| 1 | "use strict"; |
| 2 | |
| 3 | /** |
| 4 | * @typedef {import('../runner.js')} Runner |
| 5 | * @typedef {import('../test.js')} Test |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * @module Spec |
| 10 | */ |
| 11 | /** |
| 12 | * Module dependencies. |
| 13 | */ |
| 14 | |
| 15 | var Base = require("./base"); |
| 16 | var constants = require("../runner").constants; |
| 17 | var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; |
| 18 | var EVENT_RUN_END = constants.EVENT_RUN_END; |
| 19 | var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN; |
| 20 | var EVENT_SUITE_END = constants.EVENT_SUITE_END; |
| 21 | var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL; |
| 22 | var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; |
| 23 | var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING; |
| 24 | var inherits = require("../utils").inherits; |
| 25 | var color = Base.color; |
| 26 | |
| 27 | /** |
| 28 | * Expose `Spec`. |
| 29 | */ |
| 30 | |
| 31 | exports = module.exports = Spec; |
| 32 | |
| 33 | /** |
| 34 | * Constructs a new `Spec` reporter instance. |
| 35 | * |
| 36 | * @public |
| 37 | * @class |
| 38 | * @memberof Mocha.reporters |
| 39 | * @extends Mocha.reporters.Base |
| 40 | * @param {Runner} runner - Instance triggers reporter actions. |
| 41 | * @param {Object} [options] - runner options |
| 42 | */ |
| 43 | function Spec(runner, options) { |
| 44 | Base.call(this, runner, options); |
| 45 | |
| 46 | var self = this; |
| 47 | var indents = 0; |
| 48 | var n = 0; |
| 49 | |
| 50 | function indent() { |
| 51 | return Array(indents).join(" "); |
| 52 | } |
| 53 | |
| 54 | runner.on(EVENT_RUN_BEGIN, function () { |
| 55 | Base.consoleLog(); |
| 56 | }); |
| 57 | |
| 58 | runner.on(EVENT_SUITE_BEGIN, function (suite) { |
| 59 | ++indents; |
| 60 | Base.consoleLog(color("suite", "%s%s"), indent(), suite.title); |
| 61 | }); |
| 62 | |
| 63 | runner.on(EVENT_SUITE_END, function () { |
| 64 | --indents; |
| 65 | if (indents === 1) { |
| 66 | Base.consoleLog(); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | runner.on(EVENT_TEST_PENDING, function (test) { |
| 71 | var fmt = indent() + color("pending", " - %s"); |
| 72 | Base.consoleLog(fmt, test.title); |
| 73 | }); |
| 74 | |
| 75 | runner.on(EVENT_TEST_PASS, function (test) { |
| 76 | var fmt; |
| 77 | if (test.speed === "fast") { |
| 78 | fmt = |
| 79 | indent() + |
| 80 | color("checkmark", " " + Base.symbols.ok) + |
| 81 | color("pass", " %s"); |
| 82 | Base.consoleLog(fmt, test.title); |
| 83 | } else { |
| 84 | fmt = |
| 85 | indent() + |
| 86 | color("checkmark", " " + Base.symbols.ok) + |
| 87 | color("pass", " %s") + |
| 88 | color(test.speed, " (%dms)"); |
| 89 | Base.consoleLog(fmt, test.title, test.duration); |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | runner.on(EVENT_TEST_FAIL, function (test) { |
| 94 | Base.consoleLog(indent() + color("fail", " %d) %s"), ++n, test.title); |
| 95 | }); |
| 96 | |
| 97 | runner.once(EVENT_RUN_END, self.epilogue.bind(self)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Inherit from `Base.prototype`. |
| 102 | */ |
| 103 | inherits(Spec, Base); |
| 104 | |
| 105 | Spec.description = "hierarchical & verbose [default]"; |