json.js.orig 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var Base = require('./base');
  6. /**
  7. * Expose `JSON`.
  8. */
  9. exports = module.exports = JSONReporter;
  10. /**
  11. * Initialize a new `JSON` reporter.
  12. *
  13. * @api public
  14. * @param {Runner} runner
  15. * @param {options} mocha invocation options. Invoking
  16. * `mocha -R --reporter-options output-file=asdf` yields options like:
  17. * { ... reporterOptions: { "output-file": "asdf" } ... }
  18. */
  19. function JSONReporter (runner, options) {
  20. <<<<<<< HEAD
  21. =======
  22. options = options || {};
  23. var reptOptions = options.reporterOptions || {};
  24. >>>>>>> + json ouput controls: output-file, output-object
  25. Base.call(this, runner);
  26. var self = this;
  27. var tests = [];
  28. var pending = [];
  29. var failures = [];
  30. var passes = [];
  31. runner.on('test end', function (test) {
  32. tests.push(test);
  33. });
  34. runner.on('pass', function (test) {
  35. passes.push(test);
  36. });
  37. runner.on('fail', function (test) {
  38. failures.push(test);
  39. });
  40. runner.on('pending', function (test) {
  41. pending.push(test);
  42. });
  43. runner.once('end', function () {
  44. var obj = {
  45. stats: self.stats,
  46. tests: tests.map(clean),
  47. pending: pending.map(clean),
  48. failures: failures.map(clean),
  49. passes: passes.map(clean)
  50. };
  51. runner.testResults = obj;
  52. <<<<<<< HEAD
  53. if ('output-object' in options.reporterOptions) {
  54. // Pass to reporter with: reporter("json", {"output-object": myObject})
  55. Object.assign(options.reporterOptions['output-object'], obj);
  56. } else {
  57. var text = JSON.stringify(obj, null, 2);
  58. if ('output-file' in options.reporterOptions) {
  59. // Direct output with `mocha -R --reporter-options output-file=rpt.json`
  60. try {
  61. require('fs').writeFileSync(options.reporterOptions['output-file'], text);
  62. } catch (e) {
  63. console.warn('error writing to ' + options.reporterOptions.output + ':', e);
  64. =======
  65. if ('output-object' in reptOptions) {
  66. // Pass to reporter with: reporter("json", {"output-object": myObject})
  67. Object.assign(reptOptions['output-object'], obj);
  68. } else {
  69. var text = JSON.stringify(obj, null, 2);
  70. if ('output-file' in reptOptions) {
  71. // Direct output with `mocha -R --reporter-options output-file=rpt.json`
  72. try {
  73. require('fs').writeFileSync(reptOptions['output-file'], text);
  74. } catch (e) {
  75. console.warn('error writing to ' + reptOptions.output + ':', e);
  76. >>>>>>> + json ouput controls: output-file, output-object
  77. }
  78. } else {
  79. process.stdout.write(text);
  80. }
  81. }
  82. });
  83. }
  84. /**
  85. * Return a plain-object representation of `test`
  86. * free of cyclic properties etc.
  87. *
  88. * @api private
  89. * @param {Object} test
  90. * @return {Object}
  91. */
  92. function clean (test) {
  93. return {
  94. title: test.title,
  95. fullTitle: test.fullTitle(),
  96. duration: test.duration,
  97. currentRetry: test.currentRetry(),
  98. err: errorJSON(test.err || {})
  99. };
  100. }
  101. /**
  102. * Transform `error` into a JSON object.
  103. *
  104. * @api private
  105. * @param {Error} err
  106. * @return {Object}
  107. */
  108. function errorJSON (err) {
  109. var res = {};
  110. Object.getOwnPropertyNames(err).forEach(function (key) {
  111. res[key] = err[key];
  112. }, err);
  113. return res;
  114. }