spec.js 1.5 KB

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