Skip to content

Commit 2cd763e

Browse files
committed
fix(@angular/build): ensure relative karma stack traces for test failures
The karma configuration will now automatically set the `basePath` option to the temporary output path when using the application build system's karma testing. This ensures that only the relative path of the test files is represented in the stack traces of test failures. (cherry picked from commit f780e8b)
1 parent 74d54a9 commit 2cd763e

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

packages/angular/build/src/builders/karma/application_builder.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ async function initializeApplication(
442442
await writeTestFiles(buildOutput.files, buildOptions.outputPath);
443443

444444
// We need to add this to the beginning *after* the testing framework has
445-
// prepended its files.
445+
// prepended its files. The output path is required for each since they are
446+
// added later in the test process via a plugin.
446447
const polyfillsFile: FilePattern = {
447448
pattern: `${outputPath}/polyfills.js`,
448449
included: true,
@@ -458,31 +459,33 @@ async function initializeApplication(
458459
watched: false,
459460
};
460461

462+
karmaOptions.basePath = outputPath;
463+
461464
karmaOptions.files ??= [];
462465
if (options.scripts?.length) {
463466
// This should be more granular to support named bundles.
464467
// However, it replicates the behavior of the Karma Webpack-based builder.
465468
karmaOptions.files.push({
466-
pattern: `${outputPath}/scripts.js`,
469+
pattern: `scripts.js`,
467470
watched: false,
468471
type: 'js',
469472
});
470473
}
471474

472475
karmaOptions.files.push(
473476
// Serve global setup script.
474-
{ pattern: `${outputPath}/${mainName}.js`, type: 'module', watched: false },
477+
{ pattern: `${mainName}.js`, type: 'module', watched: false },
475478
// Serve all source maps.
476-
{ pattern: `${outputPath}/*.map`, included: false, watched: false },
479+
{ pattern: `*.map`, included: false, watched: false },
477480
// These are the test entrypoints.
478-
{ pattern: `${outputPath}/spec-*.js`, type: 'module', watched: false },
481+
{ pattern: `spec-*.js`, type: 'module', watched: false },
479482
);
480483

481484
if (hasChunkOrWorkerFiles(buildOutput.files)) {
482485
karmaOptions.files.push(
483486
// Allow loading of chunk-* files but don't include them all on load.
484487
{
485-
pattern: `${outputPath}/{chunk,worker}-*.js`,
488+
pattern: `{chunk,worker}-*.js`,
486489
type: 'module',
487490
included: false,
488491
watched: false,
@@ -492,7 +495,7 @@ async function initializeApplication(
492495

493496
if (options.styles?.length) {
494497
// Serve CSS outputs on page load, these are the global styles.
495-
karmaOptions.files.push({ pattern: `${outputPath}/*.css`, type: 'css', watched: false });
498+
karmaOptions.files.push({ pattern: `*.css`, type: 'css', watched: false });
496499
}
497500

498501
const parsedKarmaConfig: Config & ConfigOptions = await karma.config.parseConfig(

tests/legacy-cli/e2e/tests/test/test-sourcemap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async function () {
2020
throw new Error('ng test should have failed.');
2121
} catch (error) {
2222
assertIsError(error);
23-
assert.match(error.message, /src\/app\/app\.component\.spec\.ts/);
23+
assert.match(error.message, /\(src\/app\/app\.component\.spec\.ts:3:27/);
2424
assert.doesNotMatch(error.message, /_karma_webpack_/);
2525
}
2626

0 commit comments

Comments
 (0)