|
| 1 | +# @code-pushup/coverage-plugin |
| 2 | + |
| 3 | +**Code PushUp plugin for tracking code coverage.** |
| 4 | + |
| 5 | +This plugin allows you to measure and track code coverage on your project. |
| 6 | + |
| 7 | +Measured coverage types are mapped to Code PushUp audits in the following way |
| 8 | + |
| 9 | +- both the score and value is in range 0-1 and represents the code coverage for all passed results (_covered / total_) |
| 10 | +- missing coverage is mapped to issues in the audit details (uncalled functions, uncovered branches or lines) |
| 11 | + |
| 12 | +## Getting started |
| 13 | + |
| 14 | +1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file. |
| 15 | + |
| 16 | +2. Prepare either existing code coverage result files or a command for a coverage tool of your choice that will generate the results, such as [Istanbul](https://github.com/istanbuljs). |
| 17 | + |
| 18 | +3. Add this plugin to the `plugins` array in your Code PushUp CLI config file (e.g. `code-pushup.config.js`). |
| 19 | + |
| 20 | + Pass coverage types you wish to track (line, branch or function), paths to the code coverage results in LCOV format and optionally define your code coverage tool to be run first. |
| 21 | + |
| 22 | + Please note that when you define the tool command, you still need to define the paths to the coverage results. |
| 23 | + |
| 24 | + The configuration will look similarly to the following: |
| 25 | + |
| 26 | + ```js |
| 27 | + import coveragePlugin from '@code-pushup/coverage-plugin'; |
| 28 | + |
| 29 | + export default { |
| 30 | + // ... |
| 31 | + plugins: [ |
| 32 | + // ... |
| 33 | + await coveragePlugin({ |
| 34 | + coverageType: ['branch', 'function', 'line'], |
| 35 | + reports: ['coverage/cli/lcov.info'], |
| 36 | + coverageToolCommand: { |
| 37 | + command: 'npx nx run-many', |
| 38 | + args: ['-t', 'test', '--coverage'], |
| 39 | + }, |
| 40 | + }), |
| 41 | + ], |
| 42 | + }; |
| 43 | + ``` |
| 44 | + |
| 45 | +4. (Optional) Reference audits which you wish to include in custom categories (use `npx code-pushup print-config` to list audits and groups). |
| 46 | + |
| 47 | + 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). |
| 48 | + |
| 49 | + Note that categories can combine multiple plugins. |
| 50 | + |
| 51 | + ```js |
| 52 | + export default { |
| 53 | + // ... |
| 54 | + categories: [ |
| 55 | + { |
| 56 | + slug: 'code-coverage', |
| 57 | + title: 'Code coverage', |
| 58 | + refs: [ |
| 59 | + { |
| 60 | + type: 'audit', |
| 61 | + plugin: 'coverage', |
| 62 | + slug: 'function-coverage', |
| 63 | + weight: 2, |
| 64 | + }, |
| 65 | + { |
| 66 | + type: 'audit', |
| 67 | + plugin: 'coverage', |
| 68 | + slug: 'branch-coverage', |
| 69 | + weight: 1, |
| 70 | + }, |
| 71 | + { |
| 72 | + type: 'audit', |
| 73 | + plugin: 'coverage', |
| 74 | + slug: 'line-coverage', |
| 75 | + weight: 1, |
| 76 | + }, |
| 77 | + // ... |
| 78 | + ], |
| 79 | + }, |
| 80 | + // ... |
| 81 | + ], |
| 82 | + }; |
| 83 | + ``` |
| 84 | + |
| 85 | +5. Run the CLI with `npx code-pushup collect` and view or upload report (refer to [CLI docs](../cli/README.md)). |
| 86 | + |
| 87 | +## About code coverage |
| 88 | + |
| 89 | +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. |
| 90 | + |
| 91 | +However, 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. |
| 92 | + |
| 93 | +### LCOV format |
| 94 | + |
| 95 | +The LCOV format was originally used by [GCOV](https://gcc.gnu.org/onlinedocs/gcc/gcov/introduction-to-gcov.html) tool for coverage results in C/C++ projects. |
| 96 | +It recognises the following entities: |
| 97 | + |
| 98 | +- TN [test name] |
| 99 | +- SF [source file] |
| 100 | +- FN [line number] [function name] |
| 101 | +- FNF [number of functions found] |
| 102 | +- FNH [number of functions hit] |
| 103 | +- FNDA [number of hits] [function name] |
| 104 | +- BRDA [line number] [block number] [branch name] [number of hits] |
| 105 | +- BRF [number of branches found] |
| 106 | +- BRH [number of branches taken] |
| 107 | +- DA [line number] [number of hits] |
| 108 | +- LF [lines found] |
| 109 | +- LH [lines hit] |
| 110 | + |
| 111 | +[Here](https://github.com/linux-test-project/lcov/issues/113#issuecomment-762335134) is the source of the information above. |
| 112 | + |
| 113 | +## Plugin architecture |
| 114 | + |
| 115 | +### Plugin configuration specification |
| 116 | + |
| 117 | +The plugin accepts the following parameters: |
| 118 | + |
| 119 | +- `coverageType`: An array of types of coverage that you wish to track. Supported values: function, branch, line. |
| 120 | +- `reports`: Array of paths to files with code coverage results. Supported formats: LCOV. |
| 121 | +- (optional) `coverageToolCommand`: If you wish to run your coverage tool to generate the results first, you may define it here. |
| 122 | +- (optional) `perfectScoreThreshold`: 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. |
| 123 | + |
| 124 | +### Audit output |
| 125 | + |
| 126 | +An audit is an aggregation of all results for one coverage type passed to the plugin. |
| 127 | + |
| 128 | +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. |
| 129 | + |
| 130 | +For instance, the following can be an audit output for line coverage. |
| 131 | + |
| 132 | +```json |
| 133 | +{ |
| 134 | + "slug": "line-coverage", |
| 135 | + "displayValue": "95 %", |
| 136 | + "score": 0.95, |
| 137 | + "value": 0.95, |
| 138 | + "details": { |
| 139 | + "issues": [ |
| 140 | + { |
| 141 | + "message": "Lines 7-9 are not covered in any test case.", |
| 142 | + "severity": "warning", |
| 143 | + "source": { |
| 144 | + "file": "src/lib/utils.ts", |
| 145 | + "position": { |
| 146 | + "startLine": 7, |
| 147 | + "endLine": 9 |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + ] |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
0 commit comments