index.js 913 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. function JsonReport(opts) {
  7. this.file = opts.file || 'coverage-final.json';
  8. this.first = true;
  9. }
  10. JsonReport.prototype.onStart = function(root, context) {
  11. this.contentWriter = context.writer.writeFile(this.file);
  12. this.contentWriter.write('{');
  13. };
  14. JsonReport.prototype.onDetail = function(node) {
  15. const fc = node.getFileCoverage();
  16. const key = fc.path;
  17. const cw = this.contentWriter;
  18. if (this.first) {
  19. this.first = false;
  20. } else {
  21. cw.write(',');
  22. }
  23. cw.write(JSON.stringify(key));
  24. cw.write(': ');
  25. cw.write(JSON.stringify(fc));
  26. cw.println('');
  27. };
  28. JsonReport.prototype.onEnd = function() {
  29. const cw = this.contentWriter;
  30. cw.println('}');
  31. cw.close();
  32. };
  33. module.exports = JsonReport;