Skip to content

Commit 52fbffc

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular/build): warn and remove jsdom launcher when used with karma
The jsdom package does not currently support execution of ESM scripts. Attempting to use the karma jsdom launcher will cause test failures with potentially unclear error messages after the tests have been built and have started to execute. The karma application builder test runner will now issue a warning describing the problem and remove the jsdom launcher from the `browsers` option. The warning will be shown while analyzing the options and prior to the actual start of the test process.
1 parent 25514b8 commit 52fbffc

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Diff for: packages/angular/build/src/builders/karma/application_builder.ts

+12
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ async function collectEntrypoints(
361361
return getTestEntrypoints(testFiles, { projectSourceRoot, workspaceRoot: context.workspaceRoot });
362362
}
363363

364+
// eslint-disable-next-line max-lines-per-function
364365
async function initializeApplication(
365366
options: KarmaBuilderOptions,
366367
context: BuilderContext,
@@ -508,6 +509,17 @@ async function initializeApplication(
508509
{ promiseConfig: true, throwErrors: true },
509510
);
510511

512+
// Check for jsdom which does not support executing ESM scripts.
513+
// If present, remove jsdom and issue a warning.
514+
const updatedBrowsers = parsedKarmaConfig.browsers?.filter((browser) => browser !== 'jsdom');
515+
if (parsedKarmaConfig.browsers?.length !== updatedBrowsers?.length) {
516+
parsedKarmaConfig.browsers = updatedBrowsers;
517+
context.logger.warn(
518+
`'jsdom' does not support ESM code execution and cannot be used for karma testing.` +
519+
` The 'jsdom' entry has been removed from the 'browsers' option.`,
520+
);
521+
}
522+
511523
// Remove the webpack plugin/framework:
512524
// Alternative would be to make the Karma plugin "smart" but that's a tall order
513525
// with managing unneeded imports etc..

Diff for: packages/angular/build/src/builders/karma/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function getBaseKarmaOptions(
6868

6969
// Convert browsers from a string to an array
7070
if (typeof options.browsers === 'string' && options.browsers) {
71-
karmaOptions.browsers = options.browsers.split(',');
71+
karmaOptions.browsers = options.browsers.split(',').map((browser) => browser.trim());
7272
} else if (options.browsers === false) {
7373
karmaOptions.browsers = [];
7474
}
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 { execute } from '../../index';
10+
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';
11+
12+
describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => {
13+
describe('Option: "browsers"', () => {
14+
it('should warn if jsdom is used', async () => {
15+
await setupTarget(harness);
16+
17+
harness.useTarget('test', {
18+
...BASE_OPTIONS,
19+
browsers: BASE_OPTIONS.browsers + ',jsdom',
20+
});
21+
22+
const { result, logs } = await harness.executeOnce();
23+
expect(result?.success).toBeTrue();
24+
expect(logs).toContain(
25+
jasmine.objectContaining({
26+
message: jasmine.stringMatching(
27+
`'jsdom' does not support ESM code execution and cannot be used for karma testing.`,
28+
),
29+
}),
30+
);
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)