json.js 1.1 KB

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