Skip to content

Commit 76b585f

Browse files
MeAkibalan-agius4
authored andcommitted
fix(@angular/build): require only the launchers the karma configuration loads
The upfront dependency validation of the unit-test builder's karma runner derives a launcher package from each browser name. This requests packages that do not exist, such as `karma-chromium-launcher` for `ChromiumHeadless` (provided by `karma-chrome-launcher`) or a package named after a custom launcher defined in a Karma configuration file, so `ng test` fails before running. (cherry picked from commit e6c29bd)
1 parent 1b1c893 commit 76b585f

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ const KarmaTestRunner: TestRunner = {
2222
checker.check('karma');
2323
checker.check('karma-jasmine');
2424

25-
// Check for browser launchers
26-
if (options.browsers?.length) {
27-
for (const browser of options.browsers) {
28-
const launcherName = `karma-${browser.toLowerCase().split('headless')[0]}-launcher`;
29-
checker.check(launcherName);
30-
}
25+
// A custom configuration loads its own launcher plugins and may define custom launchers.
26+
// The built-in configuration only loads the Chrome launcher, which also provides Chromium.
27+
if (!options.runnerConfig) {
28+
checker.check('karma-chrome-launcher');
3129
}
3230

3331
if (options.coverage.enabled) {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ import KarmaTestRunner from './index';
1111

1212
describe('KarmaTestRunner', () => {
1313
describe('validateDependencies', () => {
14-
function createOptions(coverageEnabled: boolean): NormalizedUnitTestBuilderOptions {
14+
function createOptions(
15+
coverageEnabled: boolean,
16+
options: Partial<NormalizedUnitTestBuilderOptions> = {},
17+
): NormalizedUnitTestBuilderOptions {
1518
return {
1619
projectSourceRoot: '/non-existent',
1720
coverage: { enabled: coverageEnabled },
21+
...options,
1822
} as NormalizedUnitTestBuilderOptions;
1923
}
2024

@@ -29,5 +33,24 @@ describe('KarmaTestRunner', () => {
2933
/karma-coverage/,
3034
);
3135
});
36+
37+
it('requires karma-chrome-launcher for Chromium with the built-in configuration', () => {
38+
const options = createOptions(false, { browsers: ['ChromiumHeadless'] });
39+
40+
expect(() => KarmaTestRunner.validateDependencies?.(options)).toThrowError(
41+
/^(?!.*karma-chromium-launcher).*karma-chrome-launcher/s,
42+
);
43+
});
44+
45+
it('does not require launchers for the browsers of a custom configuration', () => {
46+
const options = createOptions(false, {
47+
browsers: ['ChromeNoSandbox'],
48+
runnerConfig: 'karma.conf.js',
49+
});
50+
51+
expect(() => KarmaTestRunner.validateDependencies?.(options)).toThrowError(
52+
/^(?!.*-launcher)/s,
53+
);
54+
});
3255
});
3356
});

0 commit comments

Comments
 (0)