Simple LCOV file parser to generate JSON and JSON-summary formatted reports.
$ npm install @markusberg/lcov-parseBasic 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")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"
}
]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 }
}
}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
}
]
}
}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$ npm install && npm testor to run continuously, watching for changes:
$ npm run test:watch