spec.js 2.1 KB

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