index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 TextSummaryReport(opts) {
  7. opts = opts || {};
  8. this.file = opts.file || null;
  9. }
  10. function lineForKey(summary, key) {
  11. const metrics = summary[key];
  12. key = key.substring(0, 1).toUpperCase() + key.substring(1);
  13. if (key.length < 12) {
  14. key += ' '.substring(0, 12 - key.length);
  15. }
  16. const result = [
  17. key,
  18. ':',
  19. metrics.pct + '%',
  20. '(',
  21. metrics.covered + '/' + metrics.total,
  22. ')'
  23. ].join(' ');
  24. const skipped = metrics.skipped;
  25. if (skipped > 0) {
  26. return result + ', ' + skipped + ' ignored';
  27. }
  28. return result;
  29. }
  30. TextSummaryReport.prototype.onStart = function(node, context) {
  31. const summary = node.getCoverageSummary();
  32. const cw = context.writer.writeFile(this.file);
  33. const printLine = function(key) {
  34. const str = lineForKey(summary, key);
  35. const clazz = context.classForPercent(key, summary[key].pct);
  36. cw.println(cw.colorize(str, clazz));
  37. };
  38. cw.println('');
  39. cw.println(
  40. '=============================== Coverage summary ==============================='
  41. );
  42. printLine('statements');
  43. printLine('branches');
  44. printLine('functions');
  45. printLine('lines');
  46. cw.println(
  47. '================================================================================'
  48. );
  49. cw.close();
  50. };
  51. module.exports = TextSummaryReport;