Skip to content

Commit

Permalink
feat(plugin-coverage): get coverage paths using nx
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed Feb 13, 2024
1 parent d259c14 commit cd499ea
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
31 changes: 4 additions & 27 deletions code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
packageJsonPerformanceGroupRef,
packageJsonPlugin,
} from './dist/examples/plugins';
import coveragePlugin from './dist/packages/plugin-coverage';
import coveragePlugin, {
getNxCoveragePaths,
} from './dist/packages/plugin-coverage';
import eslintPlugin, {
eslintConfigFromNxProjects,
} from './dist/packages/plugin-eslint';
Expand Down Expand Up @@ -54,32 +56,7 @@ const config: CoreConfig = {
command: 'npx',
args: ['nx', 'run-many', '-t', 'unit-test', '--coverage'],
},
reports: [
{
resultsPath: 'coverage/cli/unit-tests/lcov.info',
pathToProject: 'packages/cli',
},
{
resultsPath: 'coverage/core/unit-tests/lcov.info',
pathToProject: 'packages/core',
},
{
resultsPath: 'coverage/models/unit-tests/lcov.info',
pathToProject: 'packages/models',
},
{
resultsPath: 'coverage/utils/unit-tests/lcov.info',
pathToProject: 'packages/utils',
},
{
resultsPath: 'coverage/plugin-eslint/unit-tests/lcov.info',
pathToProject: 'packages/plugin-eslint',
},
{
resultsPath: 'coverage/plugin-coverage/unit-tests/lcov.info',
pathToProject: 'packages/plugin-coverage',
},
],
reports: await getNxCoveragePaths(['unit-test', 'integration-test']),
}),
fileSizePlugin({
directory: './dist/examples/react-todos-app',
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-coverage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ 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 - paths to results, path to project root the results belong to. LCOV format is supported for now.
- If you have an `nx` monorepo, you can adjust 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) `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.

Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"@code-pushup/utils": "*",
"parse-lcov": "^1.0.4",
"zod": "^3.22.4"
},
"peerDependencies": {
"@nx/devkit": "^17.0.0"
},
"peerDependenciesMeta": {
"@nx/devkit": {
"optional": true
}
}
}
1 change: 1 addition & 0 deletions packages/plugin-coverage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import { coveragePlugin } from './lib/coverage-plugin';

export default coveragePlugin;
export type { CoveragePluginConfig } from './lib/config';
export { getNxCoveragePaths } from './lib/nx/coverage-paths';
47 changes: 47 additions & 0 deletions packages/plugin-coverage/src/lib/nx/coverage-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ProjectGraphProjectNode } from '@nx/devkit';
import { join } from 'node:path';
import { CoverageResult } from '../config';

/**
*
* @param coverageFolder root folder containing all coverage results
* @param targets nx targets to be used for measuring coverage, test by default
* @returns An array of coverage result information for the coverage plugin.
*/
export async function getNxCoveragePaths(
targets: string[] = ['test'],
): Promise<CoverageResult[]> {
const { createProjectGraphAsync } = await import('@nx/devkit');
const { nodes } = await createProjectGraphAsync({ exitOnError: false });

const coverageResults = targets.map(target => {
const relevantNodes = Object.values(nodes).filter(graph =>
hasNxTarget(graph, target),
);

return relevantNodes
.map(node => node.data)
.map<CoverageResult>(projectConfig => {
const { reportsDirectory } = projectConfig.targets?.[target]
?.options as {
reportsDirectory: string;
};

const rootToReportsDir = join(projectConfig.root, reportsDirectory);

return {
pathToProject: projectConfig.root,
resultsPath: join(rootToReportsDir, 'lcov.info'),
};
});
});

return coverageResults.flat();
}

function hasNxTarget(
project: ProjectGraphProjectNode,
target: string,
): boolean {
return project.data.targets != null && target in project.data.targets;
}

0 comments on commit cd499ea

Please sign in to comment.