Skip to content

Commit f780e8b

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.
1 parent 18e13e2 commit f780e8b

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
@@ -438,7 +438,8 @@ async function initializeApplication(
438438
await writeTestFiles(buildOutput.files, buildOptions.outputPath);
439439

440440
// We need to add this to the beginning *after* the testing framework has
441-
// prepended its files.
441+
// prepended its files. The output path is required for each since they are
442+
// added later in the test process via a plugin.
442443
const polyfillsFile: FilePattern = {
443444
pattern: `${outputPath}/polyfills.js`,
444445
included: true,
@@ -454,31 +455,33 @@ async function initializeApplication(
454455
watched: false,
455456
};
456457

458+
karmaOptions.basePath = outputPath;
459+
457460
karmaOptions.files ??= [];
458461
if (options.scripts?.length) {
459462
// This should be more granular to support named bundles.
460463
// However, it replicates the behavior of the Karma Webpack-based builder.
461464
karmaOptions.files.push({
462-
pattern: `${outputPath}/scripts.js`,
465+
pattern: `scripts.js`,
463466
watched: false,
464467
type: 'js',
465468
});
466469
}
467470

468471
karmaOptions.files.push(
469472
// Serve global setup script.
470-
{ pattern: `${outputPath}/${mainName}.js`, type: 'module', watched: false },
473+
{ pattern: `${mainName}.js`, type: 'module', watched: false },
471474
// Serve all source maps.
472-
{ pattern: `${outputPath}/*.map`, included: false, watched: false },
475+
{ pattern: `*.map`, included: false, watched: false },
473476
// These are the test entrypoints.
474-
{ pattern: `${outputPath}/spec-*.js`, type: 'module', watched: false },
477+
{ pattern: `spec-*.js`, type: 'module', watched: false },
475478
);
476479

477480
if (hasChunkOrWorkerFiles(buildOutput.files)) {
478481
karmaOptions.files.push(
479482
// Allow loading of chunk-* files but don't include them all on load.
480483
{
481-
pattern: `${outputPath}/{chunk,worker}-*.js`,
484+
pattern: `{chunk,worker}-*.js`,
482485
type: 'module',
483486
included: false,
484487
watched: false,
@@ -488,7 +491,7 @@ async function initializeApplication(
488491

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

494497
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)