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