Skip to content

fix(@angular/build): warn and remove jsdom launcher when used with karma #29953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/angular/build/src/builders/karma/application_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ async function collectEntrypoints(
return getTestEntrypoints(testFiles, { projectSourceRoot, workspaceRoot: context.workspaceRoot });
}

// eslint-disable-next-line max-lines-per-function
async function initializeApplication(
options: KarmaBuilderOptions,
context: BuilderContext,
Expand Down Expand Up @@ -508,6 +509,17 @@ async function initializeApplication(
{ promiseConfig: true, throwErrors: true },
);

// Check for jsdom which does not support executing ESM scripts.
// If present, remove jsdom and issue a warning.
const updatedBrowsers = parsedKarmaConfig.browsers?.filter((browser) => browser !== 'jsdom');
if (parsedKarmaConfig.browsers?.length !== updatedBrowsers?.length) {
parsedKarmaConfig.browsers = updatedBrowsers;
context.logger.warn(
`'jsdom' does not support ESM code execution and cannot be used for karma testing.` +
` The 'jsdom' entry has been removed from the 'browsers' option.`,
);
}

// Remove the webpack plugin/framework:
// Alternative would be to make the Karma plugin "smart" but that's a tall order
// with managing unneeded imports etc..
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/build/src/builders/karma/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function getBaseKarmaOptions(

// Convert browsers from a string to an array
if (typeof options.browsers === 'string' && options.browsers) {
karmaOptions.browsers = options.browsers.split(',');
karmaOptions.browsers = options.browsers.split(',').map((browser) => browser.trim());
} else if (options.browsers === false) {
karmaOptions.browsers = [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { execute } from '../../index';
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';

describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => {
describe('Option: "browsers"', () => {
it('should warn if jsdom is used', async () => {
await setupTarget(harness);

harness.useTarget('test', {
...BASE_OPTIONS,
browsers: BASE_OPTIONS.browsers + ',jsdom',
});

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).toContain(
jasmine.objectContaining({
message: jasmine.stringMatching(
`'jsdom' does not support ESM code execution and cannot be used for karma testing.`,
),
}),
);
});
});
});