json-stream.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var Base = require('./base');
  6. var JSON = require('json3');
  7. /**
  8. * Expose `List`.
  9. */
  10. exports = module.exports = List;
  11. /**
  12. * Initialize a new `List` test reporter.
  13. *
  14. * @api public
  15. * @param {Runner} runner
  16. */
  17. function List (runner) {
  18. Base.call(this, runner);
  19. var self = this;
  20. var total = runner.total;
  21. runner.on('start', function () {
  22. console.log(JSON.stringify(['start', { total: total }]));
  23. });
  24. runner.on('pass', function (test) {
  25. console.log(JSON.stringify(['pass', clean(test)]));
  26. });
  27. runner.on('fail', function (test, err) {
  28. test = clean(test);
  29. test.err = err.message;
  30. test.stack = err.stack || null;
  31. console.log(JSON.stringify(['fail', test]));
  32. });
  33. runner.on('end', function () {
  34. process.stdout.write(JSON.stringify(['end', self.stats]));
  35. });
  36. }
  37. /**
  38. * Return a plain-object representation of `test`
  39. * free of cyclic properties etc.
  40. *
  41. * @api private
  42. * @param {Object} test
  43. * @return {Object}
  44. */
  45. function clean (test) {
  46. return {
  47. title: test.title,
  48. fullTitle: test.fullTitle(),
  49. duration: test.duration,
  50. currentRetry: test.currentRetry()
  51. };
  52. }