index.js 1.7 KB

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