Skip to content

LCOV parsing

Cédric Belin edited this page Feb 22, 2025 · 6 revisions

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

import console from "node:console";
import {readFile} from "node:fs/promises";
import {Report} from "@cedx/lcov";

try {
  const report = Report.parse(await readFile("/path/to/lcov.info", "utf8"));
  console.log(`The coverage report contains ${report.sourceFiles.length} source files:`);
  console.log(JSON.stringify(report));
}
catch (error) {
  console.error(error instanceof SyntaxError ? error.message : error);
}

Note

A SyntaxError is thrown if any error occurred while parsing the coverage report.
You can also use the convenient Report.tryParse() method which returns null instead of throwing an error.

Converting the Report instance to JSON format will return a map like this:

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