Skip to content

Commit

Permalink
fix(plugin-coverage): handle absolute paths in jest/vitest coverage d…
Browse files Browse the repository at this point in the history
…irectories

absolute paths were mistakenly appended to project root before

fix #728
  • Loading branch information
matejchalk committed Jul 1, 2024
1 parent 561ed3e commit f997f86
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/plugin-coverage/src/lib/nx/coverage-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import type { JestExecutorOptions } from '@nx/jest/src/executors/jest/schema';
import type { VitestExecutorOptions } from '@nx/vite/executors';
import chalk from 'chalk';
import { join } from 'node:path';
import { isAbsolute, join } from 'node:path';
import { importEsmModule, ui } from '@code-pushup/utils';
import { CoverageResult } from '../config';

Expand Down Expand Up @@ -149,6 +149,9 @@ export async function getCoveragePathForVitest(
);
}

if (isAbsolute(reportsDirectory)) {
return join(reportsDirectory, 'lcov.info');
}
return {
pathToProject: project.root,
resultsPath: join(project.root, reportsDirectory, 'lcov.info'),
Expand Down Expand Up @@ -184,5 +187,9 @@ export async function getCoveragePathForJest(
`Jest coverage configuration at ${jestConfig} does not include LCOV report format for target ${target} in ${project.name}. Add 'lcov' format under coverageReporters.`,
);
}

if (isAbsolute(coverageDirectory)) {
return join(coverageDirectory, 'lcov.info');
}
return join(project.root, coverageDirectory, 'lcov.info');
}
30 changes: 30 additions & 0 deletions packages/plugin-coverage/src/lib/nx/coverage-paths.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ describe('getCoveragePathForVitest', () => {
),
).rejects.toThrow(/configuration .* does not include LCOV report format/);
});

it('should handle absolute path in reportsDirectory', async () => {
await expect(
getCoveragePathForVitest(
{
configFile: 'vitest-valid.config.unit.ts',
reportsDirectory: join(process.cwd(), 'coverage', 'packages', 'cli'),
},
{ name: 'cli', root: join('packages', 'cli') },
'unit-test',
),
).resolves.toBe(
join(process.cwd(), 'coverage', 'packages', 'cli', 'lcov.info'),
);
});
});

describe('getCoveragePathForJest', () => {
Expand Down Expand Up @@ -311,4 +326,19 @@ describe('getCoveragePathForJest', () => {
),
).rejects.toThrow(/configuration .* does not include LCOV report format/);
});

it('should handle absolute path in coverageDirectory', async () => {
await expect(
getCoveragePathForJest(
{
jestConfig: 'jest-valid.config.unit.ts',
coverageDirectory: join(process.cwd(), 'coverage', 'packages', 'cli'),
},
{ name: 'cli', root: join('packages', 'cli') },
'unit-test',
),
).resolves.toBe(
join(process.cwd(), 'coverage', 'packages', 'cli', 'lcov.info'),
);
});
});

0 comments on commit f997f86

Please sign in to comment.