Skip to content

markusberg/lcov-parse

Repository files navigation

LCOV file parser

node.js build coverage version

Simple LCOV file parser to generate JSON and JSON-summary formatted reports.

Installation

$ npm install @markusberg/lcov-parse

Usage

Basic usage for loading and parsing an lcov.info file:

import { loadAndParse, type CoverageReport } from "@markusberg/lcov-parse"
const json: CoverageReport = await loadAndParse("./path/to/file.info")

Parsing already loaded data

If your lcov data is already loaded into a variable you can call the parsing function directly:

import { parse, type CoverageReport } from "@markusberg/lcov-parse"
const lcovData: string = "TN:TestName\nSF:foobar.js\nend_of_record\n"
const json: CoverageReport = parse(lcovData)

The json const will now contain this data:

[
  {
    "file": "foobar.js",
    "lines": { "found": 0, "hit": 0, "details": [] },
    "functions": { "hit": 0, "found": 0, "details": [] },
    "branches": { "hit": 0, "found": 0, "details": [] },
    "title": "TestName"
  }
]

JSON-summary

The JSON-summary format is convenient for CI purposes:

import {
  loadAndParse,
  generateSummary,
  type CoverageReport,
  type CoverageSummary,
} from "@markusberg/lcov-parse"

const json: CoverageReport = await loadAndParse("./path/to/file.info")
const summary: CoverageSummary = generateSummary(json)

The summary const will now contain the following data:

{
  "foobar.js": {
    "lines": { "total": 0, "covered": 0, "pct": 100 },
    "functions": { "total": 0, "covered": 0, "pct": 100 },
    "branches": { "total": 0, "covered": 0, "pct": 100 }
  },
  "total": {
    "lines": { "total": 0, "covered": 0, "pct": 100 },
    "functions": { "total": 0, "covered": 0, "pct": 100 },
    "branches": { "total": 0, "covered": 0, "pct": 100 }
  }
}

Formatting

The generated JSON will look like this:

 {
    "title": "Test #1",
    "file": "anim-base/anim-base-coverage.js",
    "functions": {
      "hit": 23,
      "found": 29,
      "details": [
        {
          "name": "(anonymous 1)",
          "line": 7,
          "hit": 6
        },
        {
          "name": "(anonymous 2)",
          "line": 620,
          "hit": 225
        },
        {
          "name": "_end",
          "line": 516,
          "hit": 228
        }
      ]
    }
    "lines": {
      "found": 181,
      "hit": 143,
      "details": [
        {
          "line": 7,
          "hit": 6
        },
        {
          "line": 29,
          "hit": 6
        }
      ]
    }
}

CLI Usage

In addition to the mandatory file name, the script takes two optional arguments:

  • --summary: generate a json-summary instead of a full json report
  • --pretty: indents and line breaks the json output

For example:

$ npx lcov-parse --summary --pretty ./coverage/lcov.info
{
  "src/index.ts": {
    "lines": {
      "total": 218,
      "covered": 218,
      "pct": 100
    },
    "functions": {
      "total": 8,
      "covered": 8,
      "pct": 100
    },
    "branches": {
      "total": 40,
      "covered": 40,
      "pct": 100
    }
  },
  "total": {
    "lines": {
      "total": 218,
      "covered": 218,
      "pct": 100
    },
    "functions": {
      "total": 8,
      "covered": 8,
      "pct": 100
    },
    "branches": {
      "total": 40,
      "covered": 40,
      "pct": 100
    }
  }
}

or

$ cat lcov.info | xargs -0 lcov-parse

Tests

$ npm install && npm test

or to run continuously, watching for changes:

$ npm run test:watch

About

Simple LCOV file parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 9