Skip to content

Commit 8cddfcd

Browse files
committed
feat(plugin-coverage): provide coverage group
1 parent e2c0e42 commit 8cddfcd

File tree

5 files changed

+113
-29
lines changed

5 files changed

+113
-29
lines changed

code-pushup.config.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,9 @@ const config: CoreConfig = {
114114
title: 'Code coverage',
115115
refs: [
116116
{
117-
type: 'audit',
117+
type: 'group',
118118
plugin: 'coverage',
119-
slug: 'function-coverage',
120-
weight: 1,
121-
},
122-
{
123-
type: 'audit',
124-
plugin: 'coverage',
125-
slug: 'branch-coverage',
126-
weight: 1,
127-
},
128-
{
129-
type: 'audit',
130-
plugin: 'coverage',
131-
slug: 'line-coverage',
119+
slug: 'coverage',
132120
weight: 1,
133121
},
134122
],

e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap

+21
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ exports[`CLI collect > should run Code coverage plugin and create report.json 1`
153153
],
154154
"description": "Official Code PushUp code coverage plugin.",
155155
"docsUrl": "https://www.npmjs.com/package/@code-pushup/coverage-plugin/",
156+
"groups": [
157+
{
158+
"description": "Group containing all defined coverage types as audits.",
159+
"refs": [
160+
{
161+
"slug": "branch-coverage",
162+
"weight": 1,
163+
},
164+
{
165+
"slug": "function-coverage",
166+
"weight": 1,
167+
},
168+
{
169+
"slug": "line-coverage",
170+
"weight": 1,
171+
},
172+
],
173+
"slug": "coverage",
174+
"title": "Code coverage metrics",
175+
},
176+
],
156177
"icon": "folder-coverage-open",
157178
"packageName": "@code-pushup/coverage-plugin",
158179
"slug": "coverage",

packages/plugin-coverage/README.md

+55-15
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Measured coverage types are mapped to Code PushUp audits in the following way
4343
};
4444
```
4545

46-
4. (Optional) Reference audits which you wish to include in custom categories (use `npx code-pushup print-config` to list audits and groups).
46+
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).
4747

4848
💡 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).
4949

@@ -56,21 +56,9 @@ Measured coverage types are mapped to Code PushUp audits in the following way
5656
title: 'Code coverage',
5757
refs: [
5858
{
59-
type: 'audit',
60-
plugin: 'coverage',
61-
slug: 'function-coverage',
62-
weight: 2,
63-
},
64-
{
65-
type: 'audit',
59+
type: 'group',
6660
plugin: 'coverage',
67-
slug: 'branch-coverage',
68-
weight: 1,
69-
},
70-
{
71-
type: 'audit',
72-
plugin: 'coverage',
73-
slug: 'line-coverage',
61+
slug: 'coverage',
7462
weight: 1,
7563
},
7664
// ...
@@ -126,6 +114,58 @@ The plugin accepts the following parameters:
126114
- (optional) `coverageToolCommand`: If you wish to run your coverage tool to generate the results first, you may define it here.
127115
- (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.
128116

117+
### Audits and group
118+
119+
This plugin provides a group for convenient declaration in your config. When defined this way, all measured coverage type audits have the same weight.
120+
121+
```ts
122+
// ...
123+
categories: [
124+
{
125+
slug: 'code-coverage',
126+
title: 'Code coverage',
127+
refs: [
128+
{
129+
type: 'group',
130+
plugin: 'coverage',
131+
slug: 'coverage',
132+
weight: 1,
133+
},
134+
// ...
135+
],
136+
},
137+
// ...
138+
],
139+
```
140+
141+
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:
142+
143+
```ts
144+
// ...
145+
categories: [
146+
{
147+
slug: 'code-coverage',
148+
title: 'Code coverage',
149+
refs: [
150+
{
151+
type: 'audit',
152+
plugin: 'coverage',
153+
slug: 'function-coverage',
154+
weight: 2,
155+
},
156+
{
157+
type: 'audit',
158+
plugin: 'coverage',
159+
slug: 'branch-coverage',
160+
weight: 1,
161+
},
162+
// ...
163+
],
164+
},
165+
// ...
166+
],
167+
```
168+
129169
### Audit output
130170

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

packages/plugin-coverage/src/lib/coverage-plugin.integration.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('coveragePlugin', () => {
2222
slug: 'coverage',
2323
title: 'Code coverage',
2424
audits: expect.any(Array),
25+
groups: expect.any(Array),
2526
}),
2627
);
2728
});
@@ -46,6 +47,31 @@ describe('coveragePlugin', () => {
4647
);
4748
});
4849

50+
it('should provide a group from defined coverage types', () => {
51+
expect(
52+
coveragePlugin({
53+
coverageTypes: ['branch', 'line'],
54+
reports: [{ resultsPath: LCOV_PATH }],
55+
}),
56+
).toStrictEqual(
57+
expect.objectContaining({
58+
audits: [
59+
expect.objectContaining({ slug: 'branch-coverage' }),
60+
expect.objectContaining({ slug: 'line-coverage' }),
61+
],
62+
groups: [
63+
expect.objectContaining({
64+
slug: 'coverage',
65+
refs: [
66+
expect.objectContaining({ slug: 'branch-coverage' }),
67+
expect.objectContaining({ slug: 'line-coverage' }),
68+
],
69+
}),
70+
],
71+
}),
72+
);
73+
});
74+
4975
it('should assign RunnerConfig when a command is passed', () => {
5076
expect(
5177
coveragePlugin({

packages/plugin-coverage/src/lib/coverage-plugin.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { join } from 'node:path';
22
import type {
33
Audit,
4+
Group,
45
PluginConfig,
56
RunnerConfig,
67
RunnerFunction,
@@ -46,6 +47,13 @@ export function coveragePlugin(config: CoveragePluginConfig): PluginConfig {
4647
}),
4748
);
4849

50+
const group: Group = {
51+
slug: 'coverage',
52+
title: 'Code coverage metrics',
53+
description: 'Group containing all defined coverage types as audits.',
54+
refs: audits.map(audit => ({ ...audit, weight: 1 })),
55+
};
56+
4957
const getAuditOutputs = async () =>
5058
perfectScoreThreshold
5159
? applyMaxScoreAboveThreshold(
@@ -75,6 +83,7 @@ export function coveragePlugin(config: CoveragePluginConfig): PluginConfig {
7583
packageName: name,
7684
version,
7785
audits,
86+
groups: [group],
7887
runner,
7988
};
8089
}

0 commit comments

Comments
 (0)