@code-pushup/coverage-plugin

中文文档
0.126.4发布于 2026/09/11MITTypeScriptES Module构建来源已验证近 30 天下载 2057

Code PushUp plugin for tracking code coverage ☂

原始使用文档
文档目录9 节
  1. Getting started
  2. About code coverage
  3. LCOV format
  4. Plugin architecture
  5. Plugin configuration specification
  6. Audits and group
  7. Audit output
  8. Coverage results alteration
  9. Providing coverage results in Nx monorepo

@code-pushup/coverage-plugin

npm downloads dependencies

🧪 Code PushUp plugin for tracking code coverage. ☂️

This plugin allows you to measure and track code coverage on your project. It accepts the LCOV coverage format and merges coverage results from any test suites provided.

Measured coverage types are mapped to Code PushUp audits in the following way

  • The value is in range 0-100 and represents the code coverage for all passed results (covered / total)
  • the score is value converted to 0-1 range
  • missing coverage is mapped to issues in the audit details (uncalled functions, uncovered branches or lines)

[!IMPORTANT] In order to successfully run your coverage tool and gather coverage results directly within the plugin, all your tests need to pass!

Getting started

  1. If you haven't already, install @code-pushup/cli and create a configuration file.

  2. Prepare either existing code coverage result files or a command for a coverage tool of your choice that will generate the results. Set lcov as the reporter to the configuration (example for Jest here).

  3. Add this plugin to the plugins array in your Code PushUp CLI config file (e.g. code-pushup.config.js).

    Pass paths to the code coverage results in LCOV format and optionally define your code coverage tool to be run first. All coverage types are measured by default. If you wish to focus on a subset of offered types of coverage, define them in coverageTypes.

    📌 Please note that when you define the tool command, you still need to define the paths to all relevant coverage results.

    The configuration will look similarly to the following:

    import coveragePlugin from '@code-pushup/coverage-plugin';
    
    export default {
      // ...
      plugins: [
        // ...
        await coveragePlugin({
          reports: ['coverage/lcov.info'],
          coverageToolCommand: {
            command: 'npx',
            args: ['jest', '--coverage', '--coverageReporters=lcov'],
          },
        }),
      ],
    };
    
  4. (Optional) Reference individual audits or the provided plugin group which you wish to include in custom categories (use npx code-pushup print-config to list audits and groups).

    💡 Assign weights based on what influence each coverage type should have on the overall category score (assign weight 0 to only include as extra info, without influencing category score).

    export default {
      // ...
      categories: [
        {
          slug: 'code-coverage',
          title: 'Code coverage',
          refs: [
            {
              type: 'group',
              plugin: 'coverage',
              slug: 'coverage',
              weight: 1,
            },
            // ...
          ],
        },
        // ...
      ],
    };
    
  5. Run the CLI with npx code-pushup collect and view or upload report (refer to CLI docs).

About code coverage

Code coverage is a metric that indicates what percentage of source code is executed by unit tests. It can give insights into test effectiveness and uncover parts of source code that would otherwise go untested.

  • Statement coverage: Measures how many statements are executed in at least one test.
  • Line coverage: Measures how many lines are executed in at least one test. Unlike statement coverage, any partially executed line counts towards line coverage.
  • Condition coverage: Measures all condition values (true/false) evaluated for a conditional statement in at least one test.
  • Branch coverage: Measures how many branches are executed as a result of conditional statements (if/else and other) in at least one test. In case of short-circuit logic, only executed paths are counted in. Unlike condition coverage, it does not ensure all combinations of condition values are tested.
  • Function coverage: Measures how many functions are called in at least one test. Argument values, usage of optional arguments or default values is irrelevant for this metric.

[!IMPORTANT] Please note that code coverage is not the same as test coverage. Test coverage measures the amount of acceptance criteria covered by tests and is hard to formally verify. This means that code coverage cannot guarantee that the designed software caters to the business requirements.

If you want to know more code coverage and how each type of coverage is measured, go to Software Testing Help.

LCOV format

The LCOV format was originally used by GCOV tool for coverage results in C/C++ projects. It recognises the following entities:

  • TN [test name]
  • SF [source file]
  • FN [line number] [function name]
  • FNF [number of functions found]
  • FNH [number of functions hit]
  • FNDA [number of hits] [function name]
  • BRDA [line number] [block number] [branch name] [number of hits]
  • BRF [number of branches found]
  • BRH [number of branches taken]
  • DA [line number] [number of hits]
  • LF [lines found]
  • LH [lines hit]

Here is the source of the information above.

[!NOTE] Branch name is usually a number indexed from 0, indicating either truthy/falsy condition or loop conditions.

Plugin architecture

Plugin configuration specification

The plugin accepts the following parameters:

  • coverageTypes: An array of types of coverage that you wish to track. Supported values: function, branch, line. Defaults to all available types.
  • reports: Array of information about files with code coverage results. LCOV format is supported for now.
    • For a single project, providing paths to results as strings is enough.
    • If you have a monorepo, both path to results (resultsPath) and path from the root to project the results belong to (pathToProject) need to be provided for the LCOV format. For Nx monorepos, you can use our helper function getNxCoveragePaths to get the path information automatically.
  • (optional) coverageToolCommand: If you wish to run your coverage tool to generate the results first, you may define it here.
  • (optional) scoreTargets: If your coverage goal is not 100%, you may define it here in range 0-1. Any score above the defined threshold will be given the perfect score. The value will stay unaffected.

Audits and group

This plugin provides a group for convenient declaration in your config. When defined this way, all measured coverage type audits have the same weight.

     // ...
     categories: [
       {
         slug: 'code-coverage',
         title: 'Code coverage',
         refs: [
           {
             type: 'group',
             plugin: 'coverage',
             slug: 'coverage',
             weight: 1,
           },
           // ...
         ],
       },
       // ...
     ],

Each coverage type still has its own audit. So when you want to include a subset of coverage types or assign different weights to them, you can do so in the following way:

     // ...
     categories: [
       {
         slug: 'code-coverage',
         title: 'Code coverage',
         refs: [
           {
             type: 'audit',
             plugin: 'coverage',
             slug: 'function-coverage',
             weight: 2,
           },
           {
             type: 'audit',
             plugin: 'coverage',
             slug: 'branch-coverage',
             weight: 1,
           },
           // ...
         ],
       },
       // ...
     ],

Audit output

An audit is an aggregation of all results for one coverage type passed to the plugin.

For functions and branches, an issue points to a single instance of a branch or function not covered in any test and counts as an error. In line coverage, one issue groups any amount of consecutive lines together to reduce the total amount of issues and counts as a warning.

For instance, the following can be an audit output for line coverage.

{
  "slug": "line-coverage",
  "displayValue": "95 %",
  "score": 0.95,
  "value": 95,
  "details": {
    "issues": [
      {
        "message": "Lines 7-9 are not covered in any test case.",
        "severity": "warning",
        "source": {
          "file": "packages/cli/src/lib/utils.ts",
          "position": {
            "startLine": 7,
            "endLine": 9
          }
        }
      }
    ]
  }
}

Coverage results alteration

At the moment, the LCOV results include (empty-report) functions with missing coverage. These point to various imports or exports, not actual functions. For that reason, they are omitted from the results.

Providing coverage results in Nx monorepo

As a part of the plugin, there is a getNxCoveragePaths helper for setting up paths to coverage results if you are using Nx. The helper accepts all relevant targets (e.g. test or unit-test) and searches for a coverage path option. Jest and Vitest configuration options are currently supported:

  • For @nx/jest executor it looks for the coverageDirectory option.
  • For @nx/vite executor it looks for the reportsDirectory option.

[!IMPORTANT] Please note that you need to set up the coverage directory option in your project.json target options. Test configuration files are not searched.

版本与发布记录

@code-pushup/coverage-plugin 共发布 179 个版本,首次发布于 2024/02/05;过去 12 个月发布了 62 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.126.4latest2026/09/111 个月前
0.126.32026/05/195 个月前
0.126.22026/03/317 个月前
0.126.12026/03/317 个月前
0.126.02026/03/307 个月前
0.125.02026/03/277 个月前

关于 @code-pushup/coverage-plugin 的常见问题

@code-pushup/coverage-plugin 是什么?

Code PushUp plugin for tracking code coverage ☂

如何安装 @code-pushup/coverage-plugin?

在项目目录运行 npm install @code-pushup/coverage-plugin,或使用 pnpm add / yarn add / bun add。

@code-pushup/coverage-plugin 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@code-pushup/coverage-plugin 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@code-pushup/coverage-plugin 可以商用吗?

@code-pushup/coverage-plugin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@code-pushup/coverage-plugin 的下载量有多少?

根据 npm 官方下载统计,@code-pushup/coverage-plugin 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,057 次,日均约 69 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@code-pushup/coverage-plugin 还在维护吗?

最新版本 0.126.4 发布于 2026/09/11(1 个月前);至今共发布 179 个版本,其中过去 12 个月发布了 62 个;首次发布于 2024/02/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。