Skip to content

Commit cd499ea

Browse files
committed
feat(plugin-coverage): get coverage paths using nx
1 parent d259c14 commit cd499ea

File tree

5 files changed

+61
-27
lines changed

5 files changed

+61
-27
lines changed

code-pushup.config.ts

+4-27
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
packageJsonPerformanceGroupRef,
1212
packageJsonPlugin,
1313
} from './dist/examples/plugins';
14-
import coveragePlugin from './dist/packages/plugin-coverage';
14+
import coveragePlugin, {
15+
getNxCoveragePaths,
16+
} from './dist/packages/plugin-coverage';
1517
import eslintPlugin, {
1618
eslintConfigFromNxProjects,
1719
} from './dist/packages/plugin-eslint';
@@ -54,32 +56,7 @@ const config: CoreConfig = {
5456
command: 'npx',
5557
args: ['nx', 'run-many', '-t', 'unit-test', '--coverage'],
5658
},
57-
reports: [
58-
{
59-
resultsPath: 'coverage/cli/unit-tests/lcov.info',
60-
pathToProject: 'packages/cli',
61-
},
62-
{
63-
resultsPath: 'coverage/core/unit-tests/lcov.info',
64-
pathToProject: 'packages/core',
65-
},
66-
{
67-
resultsPath: 'coverage/models/unit-tests/lcov.info',
68-
pathToProject: 'packages/models',
69-
},
70-
{
71-
resultsPath: 'coverage/utils/unit-tests/lcov.info',
72-
pathToProject: 'packages/utils',
73-
},
74-
{
75-
resultsPath: 'coverage/plugin-eslint/unit-tests/lcov.info',
76-
pathToProject: 'packages/plugin-eslint',
77-
},
78-
{
79-
resultsPath: 'coverage/plugin-coverage/unit-tests/lcov.info',
80-
pathToProject: 'packages/plugin-coverage',
81-
},
82-
],
59+
reports: await getNxCoveragePaths(['unit-test', 'integration-test']),
8360
}),
8461
fileSizePlugin({
8562
directory: './dist/examples/react-todos-app',

packages/plugin-coverage/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ The plugin accepts the following parameters:
117117

118118
- `coverageTypes`: An array of types of coverage that you wish to track. Supported values: `function`, `branch`, `line`. Defaults to all available types.
119119
- `reports`: Array of information about files with code coverage results - paths to results, path to project root the results belong to. LCOV format is supported for now.
120+
- If you have an `nx` monorepo, you can adjust our helper function `getNxCoveragePaths` to get the path information automatically.
120121
- (optional) `coverageToolCommand`: If you wish to run your coverage tool to generate the results first, you may define it here.
121122
- (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.
122123

packages/plugin-coverage/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@
66
"@code-pushup/utils": "*",
77
"parse-lcov": "^1.0.4",
88
"zod": "^3.22.4"
9+
},
10+
"peerDependencies": {
11+
"@nx/devkit": "^17.0.0"
12+
},
13+
"peerDependenciesMeta": {
14+
"@nx/devkit": {
15+
"optional": true
16+
}
917
}
1018
}

packages/plugin-coverage/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import { coveragePlugin } from './lib/coverage-plugin';
22

33
export default coveragePlugin;
44
export type { CoveragePluginConfig } from './lib/config';
5+
export { getNxCoveragePaths } from './lib/nx/coverage-paths';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { ProjectGraphProjectNode } from '@nx/devkit';
2+
import { join } from 'node:path';
3+
import { CoverageResult } from '../config';
4+
5+
/**
6+
*
7+
* @param coverageFolder root folder containing all coverage results
8+
* @param targets nx targets to be used for measuring coverage, test by default
9+
* @returns An array of coverage result information for the coverage plugin.
10+
*/
11+
export async function getNxCoveragePaths(
12+
targets: string[] = ['test'],
13+
): Promise<CoverageResult[]> {
14+
const { createProjectGraphAsync } = await import('@nx/devkit');
15+
const { nodes } = await createProjectGraphAsync({ exitOnError: false });
16+
17+
const coverageResults = targets.map(target => {
18+
const relevantNodes = Object.values(nodes).filter(graph =>
19+
hasNxTarget(graph, target),
20+
);
21+
22+
return relevantNodes
23+
.map(node => node.data)
24+
.map<CoverageResult>(projectConfig => {
25+
const { reportsDirectory } = projectConfig.targets?.[target]
26+
?.options as {
27+
reportsDirectory: string;
28+
};
29+
30+
const rootToReportsDir = join(projectConfig.root, reportsDirectory);
31+
32+
return {
33+
pathToProject: projectConfig.root,
34+
resultsPath: join(rootToReportsDir, 'lcov.info'),
35+
};
36+
});
37+
});
38+
39+
return coverageResults.flat();
40+
}
41+
42+
function hasNxTarget(
43+
project: ProjectGraphProjectNode,
44+
target: string,
45+
): boolean {
46+
return project.data.targets != null && target in project.data.targets;
47+
}

0 commit comments

Comments
 (0)