spec.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Module dependencies.
  3. */
  4. var Base = require('./base')
  5. , cursor = Base.cursor
  6. , color = Base.color;
  7. /**
  8. * Expose `Spec`.
  9. */
  10. exports = module.exports = Spec;
  11. /**
  12. * Initialize a new `Spec` test reporter.
  13. *
  14. * @param {Runner} runner
  15. * @api public
  16. */
  17. function Spec(runner) {
  18. Base.call(this, runner);
  19. var self = this
  20. , stats = this.stats
  21. , indents = 0
  22. , 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(suite){
  34. --indents;
  35. if (1 == indents) console.log();
  36. });
  37. runner.on('test', function(test){
  38. process.stdout.write(indent() + color('pass', ' ◦ ' + test.title + ': '));
  39. });
  40. runner.on('pending', function(test){
  41. var fmt = indent() + color('pending', ' - %s');
  42. console.log(fmt, test.title);
  43. });
  44. runner.on('pass', function(test){
  45. if ('fast' == test.speed) {
  46. var fmt = indent()
  47. + color('checkmark', ' ' + Base.symbols.ok)
  48. + color('pass', ' %s ');
  49. cursor.CR();
  50. console.log(fmt, test.title);
  51. } else {
  52. var 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, err){
  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. Spec.prototype.__proto__ = Base.prototype;