Istanbul Coverage Threshold Checker

0.2.1 · abandoned · verified Sun Apr 19

This package, `istanbul-threshold-checker`, provides a utility for validating code coverage reports generated by Istanbul against user-defined thresholds. It allows developers to enforce minimum coverage percentages or maximum allowed uncovered entities for statements, branches, lines, and functions. Thresholds can be configured globally for the entire project or on a per-file basis, offering granular control over coverage requirements. Last published in November 2016 at version 0.2.1, this module is considered abandoned. Its core functionality for checking coverage thresholds has since been integrated directly into the `istanbul` project's command-line interface, `nyc`, as well as into popular testing frameworks like Jest. Modern projects are advised to use `nyc`'s native threshold checking capabilities or those provided by their testing framework rather than relying on this standalone, unmaintained package. It strictly uses CommonJS `require` syntax and depends on an older version of `istanbul`, which may lead to compatibility issues with contemporary coverage formats.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to manually collect a coverage report using `istanbul` and then use `istanbul-threshold-checker` to validate it against defined global and per-file thresholds.

/* package.json (excerpt) */
// {
//   "dependencies": {
//     "istanbul": "^0.4.0", // Use a compatible istanbul version
//     "istanbul-threshold-checker": "^0.2.0"
//   }
// }

const istanbul = require('istanbul');
const checker = require('istanbul-threshold-checker');

// Mock a coverage JSON object (replace with actual report)
const coverageJsonObject = {
  '/path/to/my-file.js': {
    path: '/path/to/my-file.js',
    s: { '1': 1, '2': 1, '3': 0 }, // Statements
    b: { '1': [1, 0] },         // Branches
    f: { '1': 1 },             // Functions
    l: { '1': 1, '2': 1, '3': 0 }  // Lines
  }
};

const collector = new istanbul.Collector();
collector.add(coverageJsonObject);
const coverage = collector.getFinalCoverage();

const thresholds = {
  global: {
    statements: 50,
    branches: 50,
    lines: 50,
    functions: 50
  },
  each: {
    statements: 0, // 0% means any file with less than 0% statements coverage fails (always passes if some coverage exists)
    branches: -5,  // No more than 5 uncovered branches per file
    lines: 70,
    functions: 100
  }
};

const results = checker.checkFailures(thresholds, coverage);

if (results.some(r => r.global?.failed || r.each?.failed)) {
  console.error('Coverage thresholds failed:');
  console.error(JSON.stringify(results, null, 2));
  // process.exit(1);
} else {
  console.log('Coverage thresholds passed!');
  console.log(JSON.stringify(results, null, 2));
}

/* Example of 'results' output:
[
  {
    "type": "statements",
    "global": {
      "failed": false,
      "value": 66.66666666666667
    },
    "each": {
      "failed": true,
      "failures": [
        "/path/to/my-file.js"
      ]
    }
  },
  {
    "type": "branches",
    "global": {
      "failed": true,
      "value": 50
    },
    "each": {
      "failed": false
    }
  },
  {
    "type": "functions",
    "global": {
      "failed": false,
      "value": 100
    },
    "each": {
      "failed": false
    }
  },
  {
    "type": "lines",
    "global": {
      "failed": false,
      "value": 66.66666666666667
    },
    "each": {
      "failed": true,
      "failures": [
        "/path/to/my-file.js"
      ]
    }
  }
]
*/

view raw JSON →