tap.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var Base = require('./base');
  6. /**
  7. * Expose `TAP`.
  8. */
  9. exports = module.exports = TAP;
  10. /**
  11. * Initialize a new `TAP` reporter.
  12. *
  13. * @api public
  14. * @param {Runner} runner
  15. */
  16. function TAP (runner) {
  17. Base.call(this, runner);
  18. var n = 1;
  19. var passes = 0;
  20. var failures = 0;
  21. runner.on('start', function () {
  22. var total = runner.grepTotal(runner.suite);
  23. console.log('%d..%d', 1, total);
  24. });
  25. runner.on('test end', function () {
  26. ++n;
  27. });
  28. runner.on('pending', function (test) {
  29. console.log('ok %d %s # SKIP -', n, title(test));
  30. });
  31. runner.on('pass', function (test) {
  32. passes++;
  33. console.log('ok %d %s', n, title(test));
  34. });
  35. runner.on('fail', function (test, err) {
  36. failures++;
  37. console.log('not ok %d %s', n, title(test));
  38. if (err.stack) {
  39. console.log(err.stack.replace(/^/gm, ' '));
  40. }
  41. });
  42. runner.on('end', function () {
  43. console.log('# tests ' + (passes + failures));
  44. console.log('# pass ' + passes);
  45. console.log('# fail ' + failures);
  46. });
  47. }
  48. /**
  49. * Return a TAP-safe title of `test`
  50. *
  51. * @api private
  52. * @param {Object} test
  53. * @return {String}
  54. */
  55. function title (test) {
  56. return test.fullTitle().replace(/#/g, '');
  57. }