coverage-summary.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 percent = require('./percent');
  7. const dataProperties = require('./data-properties');
  8. function blankSummary() {
  9. const empty = () => ({
  10. total: 0,
  11. covered: 0,
  12. skipped: 0,
  13. pct: 'Unknown'
  14. });
  15. return {
  16. lines: empty(),
  17. statements: empty(),
  18. functions: empty(),
  19. branches: empty(),
  20. branchesTrue: empty()
  21. };
  22. }
  23. // asserts that a data object "looks like" a summary coverage object
  24. function assertValidSummary(obj) {
  25. const valid =
  26. obj && obj.lines && obj.statements && obj.functions && obj.branches;
  27. if (!valid) {
  28. throw new Error(
  29. 'Invalid summary coverage object, missing keys, found:' +
  30. Object.keys(obj).join(',')
  31. );
  32. }
  33. }
  34. /**
  35. * CoverageSummary provides a summary of code coverage . It exposes 4 properties,
  36. * `lines`, `statements`, `branches`, and `functions`. Each of these properties
  37. * is an object that has 4 keys `total`, `covered`, `skipped` and `pct`.
  38. * `pct` is a percentage number (0-100).
  39. */
  40. class CoverageSummary {
  41. /**
  42. * @constructor
  43. * @param {Object|CoverageSummary} [obj=undefined] an optional data object or
  44. * another coverage summary to initialize this object with.
  45. */
  46. constructor(obj) {
  47. if (!obj) {
  48. this.data = blankSummary();
  49. } else if (obj instanceof CoverageSummary) {
  50. this.data = obj.data;
  51. } else {
  52. this.data = obj;
  53. }
  54. assertValidSummary(this.data);
  55. }
  56. /**
  57. * merges a second summary coverage object into this one
  58. * @param {CoverageSummary} obj - another coverage summary object
  59. */
  60. merge(obj) {
  61. const keys = [
  62. 'lines',
  63. 'statements',
  64. 'branches',
  65. 'functions',
  66. 'branchesTrue'
  67. ];
  68. keys.forEach(key => {
  69. if (obj[key]) {
  70. this[key].total += obj[key].total;
  71. this[key].covered += obj[key].covered;
  72. this[key].skipped += obj[key].skipped;
  73. this[key].pct = percent(this[key].covered, this[key].total);
  74. }
  75. });
  76. return this;
  77. }
  78. /**
  79. * returns a POJO that is JSON serializable. May be used to get the raw
  80. * summary object.
  81. */
  82. toJSON() {
  83. return this.data;
  84. }
  85. /**
  86. * return true if summary has no lines of code
  87. */
  88. isEmpty() {
  89. return this.lines.total === 0;
  90. }
  91. }
  92. dataProperties(CoverageSummary, [
  93. 'lines',
  94. 'statements',
  95. 'functions',
  96. 'branches',
  97. 'branchesTrue'
  98. ]);
  99. module.exports = {
  100. CoverageSummary
  101. };