Skip to content

Commit 77fb7fa

Browse files
authored
fix(@angular/build): only require karma-coverage when coverage is enabled
The upfront dependency validation of the unit-test builder's karma runner checks the truthiness of the normalized `coverage` option, which is always an object since the introduction of the advanced coverage options. As a result, `ng test` with the karma runner fails with a `MissingDependenciesError` requesting the `karma-coverage` package even when coverage is disabled. Check the `enabled` property instead, matching the vitest runner and the karma executor. A new unit spec for the karma runner's dependency validation fails without this change.
1 parent 9d6baa3 commit 77fb7fa

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎packages/angular/build/src/builders/unit-test/runners/karma/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const KarmaTestRunner: TestRunner = {
3030
}
3131
}
3232

33-
if (options.coverage) {
33+
if (options.coverage.enabled) {
3434
checker.check('karma-coverage');
3535
}
3636

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import type { NormalizedUnitTestBuilderOptions } from '../../options';
10+
import KarmaTestRunner from './index';
11+
12+
describe('KarmaTestRunner', () => {
13+
describe('validateDependencies', () => {
14+
function createOptions(coverageEnabled: boolean): NormalizedUnitTestBuilderOptions {
15+
return {
16+
projectSourceRoot: '/non-existent',
17+
coverage: { enabled: coverageEnabled },
18+
} as NormalizedUnitTestBuilderOptions;
19+
}
20+
21+
it('does not require karma-coverage when coverage is disabled', () => {
22+
expect(() => KarmaTestRunner.validateDependencies?.(createOptions(false))).toThrowError(
23+
/^(?!.*karma-coverage)/s,
24+
);
25+
});
26+
27+
it('requires karma-coverage when coverage is enabled', () => {
28+
expect(() => KarmaTestRunner.validateDependencies?.(createOptions(true))).toThrowError(
29+
/karma-coverage/,
30+
);
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)