Skip to content

Commit f9718bd

Browse files
committed
fix(plugin-coverage): skip lcov reporter validation if jest config uses preset
reporter could be configured in preset fix #727
1 parent f997f86 commit f9718bd

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,22 @@ export async function getCoveragePathForJest(
165165
) {
166166
const { jestConfig } = options;
167167

168-
const { coverageDirectory, coverageReporters } =
169-
options.coverageDirectory && options.coverageReporters
170-
? options // skip import if both overrides available
171-
: {
172-
...(await importEsmModule<JestCoverageConfig>({
173-
filepath: jestConfig,
174-
format: 'cjs',
175-
})),
176-
...options,
177-
};
168+
const testConfig = await importEsmModule<JestCoverageConfig>({
169+
filepath: jestConfig,
170+
format: 'cjs',
171+
});
172+
const { coverageDirectory, coverageReporters } = {
173+
...testConfig,
174+
...options,
175+
};
178176

179177
if (coverageDirectory == null) {
180178
throw new Error(
181179
`Jest coverage configuration at ${jestConfig} does not include coverage path for target ${target} in ${project.name}. Add the path under coverageDirectory.`,
182180
);
183181
}
184182

185-
if (!coverageReporters?.includes('lcov')) {
183+
if (!coverageReporters?.includes('lcov') && !('preset' in testConfig)) {
186184
throw new Error(
187185
`Jest coverage configuration at ${jestConfig} does not include LCOV report format for target ${target} in ${project.name}. Add 'lcov' format under coverageReporters.`,
188186
);

packages/plugin-coverage/src/lib/nx/coverage-paths.unit.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ vi.mock('bundle-require', () => ({
5151
coverageDirectory: 'coverage',
5252
};
5353

54+
const JEST_PRESET: JestCoverageConfig & { preset?: string } = {
55+
preset: '../../jest.preset.ts',
56+
coverageDirectory: 'coverage',
57+
};
58+
5459
const wrapReturnValue = (
5560
value: VitestCoverageConfig | JestCoverageConfig,
5661
) => ({ mod: { default: value } });
@@ -69,6 +74,8 @@ vi.mock('bundle-require', () => ({
6974
return wrapReturnValue(JEST_NO_LCOV);
7075
case 'jest-no-dir':
7176
return wrapReturnValue(JEST_NO_DIR);
77+
case 'jest-preset':
78+
return wrapReturnValue(JEST_PRESET);
7279
default:
7380
return wrapReturnValue({});
7481
}
@@ -327,6 +334,29 @@ describe('getCoveragePathForJest', () => {
327334
).rejects.toThrow(/configuration .* does not include LCOV report format/);
328335
});
329336

337+
it('should not throw if lcov reporter in both project.json and jest config', async () => {
338+
await expect(
339+
getCoveragePathForJest(
340+
{
341+
jestConfig: 'jest-valid.config.integration.ts',
342+
coverageReporters: ['lcov'],
343+
},
344+
{ name: 'core', root: join('packages', 'core') },
345+
'integration-test',
346+
),
347+
).resolves.toBeTypeOf('string');
348+
});
349+
350+
it('should not throw regarding missing lcov reporter if jest config uses preset', async () => {
351+
await expect(
352+
getCoveragePathForJest(
353+
{ jestConfig: 'jest-preset.config.ts' },
354+
{ name: 'core', root: join('packages', 'core') },
355+
'test',
356+
),
357+
).resolves.toBeTypeOf('string');
358+
});
359+
330360
it('should handle absolute path in coverageDirectory', async () => {
331361
await expect(
332362
getCoveragePathForJest(

0 commit comments

Comments
 (0)