spec.js 2.4 KB

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