Home Manual Reference Source Test

LCOV Reports for JS

Runtime Release License Downloads Dependencies Coverage Build

Parse and format LCOV coverage reports, in JavaScript.

Requirements

The latest Node.js and npm versions. If you plan to play with the sources, you will also need the latest Gulp.js version.

Installing via npm

From a command prompt, run:

$ npm install --save @cedx/lcov

Usage

This package provides a set of classes representing a coverage report and its data. The Report class, the main one, provides the parsing and formatting features.

Parse coverage data from a LCOV file

The Report.parse() static method parses a coverage report provided as string, and returns a Report instance giving detailed information about this coverage report:

const {readFileSync} = require('fs');
const {Report} = require('@cedx/lcov');

try {
  let coverage = readFileSync('lcov.info', 'utf8');
  let report = Report.parse(coverage);
  console.log(`The coverage report contains ${report.records.length} records:`);
  console.log(report.toJSON());
}

catch (error) {
  console.log('The LCOV report has an invalid format.');
}

The Report.toJSON() instance method will return a map like this:

{
  "testName": "Example",
  "records": [
    {
      "sourceFile": "/home/cedx/lcov.js/fixture.js",
      "branches": {
        "data": [],
        "found": 0,
        "hit": 0
      },
      "functions": {
        "data": [
          {"executionCount": 2, "functionName": "main", "lineNumber": 4}
        ],
        "found": 1,
        "hit": 1
      },
      "lines": {
        "data": [
          {"checksum": "PF4Rz2r7RTliO9u6bZ7h6g", "executionCount": 2, "lineNumber": 6},
          {"checksum": "y7GE3Y4FyXCeXcrtqgSVzw", "executionCount": 2, "lineNumber": 9}
        ],
        "found": 2,
        "hit": 2
      }
    }
  ]
}

Format coverage data to the LCOV format

Each provided class has a dedicated toString() instance method returning the corresponding data formatted as LCOV string. All you have to do is to create the adequate structure using these different classes, and to export the final result:

const {FunctionCoverage, LineCoverage, LineData, Record, Report} = require('@cedx/lcov');

let record = new Record('/home/cedx/lcov.js/fixture.js');
record.functions = new FunctionCoverage(1, 1);
record.lines = new LineCoverage(2, 2, [
 new LineData(6, 2, 'PF4Rz2r7RTliO9u6bZ7h6g'),
 new LineData(7, 2, 'yGMB6FhEEAd8OyASe3Ni1w')
]);

let report = new Report('Example', [record]);
console.log(report.toString());

The Report.toString() method will return a LCOV report formatted like this:

TN:Example
SF:/home/cedx/lcov.js/fixture.js
FNF:1
FNH:1
DA:6,2,PF4Rz2r7RTliO9u6bZ7h6g
DA:7,2,yGMB6FhEEAd8OyASe3Ni1w
LF:2
LH:2
end_of_record

See also

License

LCOV Reports for JS is distributed under the Apache License, version 2.0.