Skip to content

Commit

Permalink
feat(plugin-coverage): provide coverage group
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed Feb 13, 2024
1 parent e2c0e42 commit 8cddfcd
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 29 deletions.
16 changes: 2 additions & 14 deletions code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,9 @@ const config: CoreConfig = {
title: 'Code coverage',
refs: [
{
type: 'audit',
type: 'group',
plugin: 'coverage',
slug: 'function-coverage',
weight: 1,
},
{
type: 'audit',
plugin: 'coverage',
slug: 'branch-coverage',
weight: 1,
},
{
type: 'audit',
plugin: 'coverage',
slug: 'line-coverage',
slug: 'coverage',
weight: 1,
},
],
Expand Down
21 changes: 21 additions & 0 deletions e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ exports[`CLI collect > should run Code coverage plugin and create report.json 1`
],
"description": "Official Code PushUp code coverage plugin.",
"docsUrl": "https://www.npmjs.com/package/@code-pushup/coverage-plugin/",
"groups": [
{
"description": "Group containing all defined coverage types as audits.",
"refs": [
{
"slug": "branch-coverage",
"weight": 1,
},
{
"slug": "function-coverage",
"weight": 1,
},
{
"slug": "line-coverage",
"weight": 1,
},
],
"slug": "coverage",
"title": "Code coverage metrics",
},
],
"icon": "folder-coverage-open",
"packageName": "@code-pushup/coverage-plugin",
"slug": "coverage",
Expand Down
70 changes: 55 additions & 15 deletions packages/plugin-coverage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Measured coverage types are mapped to Code PushUp audits in the following way
};
```

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

Expand All @@ -56,21 +56,9 @@ Measured coverage types are mapped to Code PushUp audits in the following way
title: 'Code coverage',
refs: [
{
type: 'audit',
plugin: 'coverage',
slug: 'function-coverage',
weight: 2,
},
{
type: 'audit',
type: 'group',
plugin: 'coverage',
slug: 'branch-coverage',
weight: 1,
},
{
type: 'audit',
plugin: 'coverage',
slug: 'line-coverage',
slug: 'coverage',
weight: 1,
},
// ...
Expand Down Expand Up @@ -126,6 +114,58 @@ The plugin accepts the following parameters:
- (optional) `coverageToolCommand`: If you wish to run your coverage tool to generate the results first, you may define it here.
- (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.

### 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.

```ts
// ...
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:

```ts
// ...
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('coveragePlugin', () => {
slug: 'coverage',
title: 'Code coverage',
audits: expect.any(Array),
groups: expect.any(Array),
}),
);
});
Expand All @@ -46,6 +47,31 @@ describe('coveragePlugin', () => {
);
});

it('should provide a group from defined coverage types', () => {
expect(
coveragePlugin({
coverageTypes: ['branch', 'line'],
reports: [{ resultsPath: LCOV_PATH }],
}),
).toStrictEqual(
expect.objectContaining({
audits: [
expect.objectContaining({ slug: 'branch-coverage' }),
expect.objectContaining({ slug: 'line-coverage' }),
],
groups: [
expect.objectContaining({
slug: 'coverage',
refs: [
expect.objectContaining({ slug: 'branch-coverage' }),
expect.objectContaining({ slug: 'line-coverage' }),
],
}),
],
}),
);
});

it('should assign RunnerConfig when a command is passed', () => {
expect(
coveragePlugin({
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-coverage/src/lib/coverage-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'node:path';
import type {
Audit,
Group,
PluginConfig,
RunnerConfig,
RunnerFunction,
Expand Down Expand Up @@ -46,6 +47,13 @@ export function coveragePlugin(config: CoveragePluginConfig): PluginConfig {
}),
);

const group: Group = {
slug: 'coverage',
title: 'Code coverage metrics',
description: 'Group containing all defined coverage types as audits.',
refs: audits.map(audit => ({ ...audit, weight: 1 })),
};

const getAuditOutputs = async () =>
perfectScoreThreshold
? applyMaxScoreAboveThreshold(
Expand Down Expand Up @@ -75,6 +83,7 @@ export function coveragePlugin(config: CoveragePluginConfig): PluginConfig {
packageName: name,
version,
audits,
groups: [group],
runner,
};
}

0 comments on commit 8cddfcd

Please sign in to comment.