spec.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Module dependencies.
  3. */
  4. var Base = require('./base');
  5. var inherits = require('../utils').inherits;
  6. var color = Base.color;
  7. var cursor = Base.cursor;
  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. cursor.CR();
  50. console.log(fmt, test.title);
  51. } else {
  52. fmt = indent()
  53. + color('checkmark', ' ' + Base.symbols.ok)
  54. + color('pass', ' %s')
  55. + color(test.speed, ' (%dms)');
  56. cursor.CR();
  57. console.log(fmt, test.title, test.duration);
  58. }
  59. });
  60. runner.on('fail', function(test) {
  61. cursor.CR();
  62. console.log(indent() + color('fail', ' %d) %s'), ++n, test.title);
  63. });
  64. runner.on('end', self.epilogue.bind(self));
  65. }
  66. /**
  67. * Inherit from `Base.prototype`.
  68. */
  69. inherits(Spec, Base);