spec.js 1.7 KB

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