index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 JsonSummaryReport(opts) {
  7. this.file = opts.file || 'coverage-summary.json';
  8. this.contentWriter = null;
  9. this.first = true;
  10. }
  11. JsonSummaryReport.prototype.onStart = function (root, context) {
  12. this.contentWriter = context.writer.writeFile(this.file);
  13. this.contentWriter.write("{");
  14. };
  15. JsonSummaryReport.prototype.writeSummary = function (filePath, sc) {
  16. var cw = this.contentWriter;
  17. if (this.first) {
  18. this.first = false;
  19. } else {
  20. cw.write(",");
  21. }
  22. cw.write(JSON.stringify(filePath));
  23. cw.write(': ');
  24. cw.write(JSON.stringify(sc));
  25. cw.println("");
  26. };
  27. JsonSummaryReport.prototype.onSummary = function (node) {
  28. if (!node.isRoot()) {
  29. return;
  30. }
  31. this.writeSummary("total", node.getCoverageSummary());
  32. };
  33. JsonSummaryReport.prototype.onDetail = function (node) {
  34. this.writeSummary(node.getFileCoverage().path, node.getCoverageSummary());
  35. };
  36. JsonSummaryReport.prototype.onEnd = function () {
  37. var cw = this.contentWriter;
  38. cw.println("}");
  39. cw.close();
  40. };
  41. module.exports = JsonSummaryReport;