json-stream.js 1.1 KB

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