json-stream.js 1.3 KB

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