diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts index fc92ed59dbc0..55a10b37c54e 100644 --- a/packages/angular/build/src/builders/karma/application_builder.ts +++ b/packages/angular/build/src/builders/karma/application_builder.ts @@ -577,6 +577,35 @@ async function initializeApplication( parsedKarmaConfig.reporters ??= []; parsedKarmaConfig.reporters.push(AngularPolyfillsPlugin.NAME); + // Adjust karma junit reporter outDir location to maintain previous (devkit) behavior + // The base path for the reporter was previously the workspace root. + // To keep the files in the same location, the reporter's output directory is adjusted + // to be relative to the workspace root when using junit. + if (parsedKarmaConfig.reporters?.some((reporter) => reporter === 'junit')) { + if ('junitReporter' in parsedKarmaConfig) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const junitReporterOptions = (parsedKarmaConfig as any)['junitReporter'] as { + outputDir?: unknown; + }; + if (junitReporterOptions.outputDir == undefined) { + junitReporterOptions.outputDir = context.workspaceRoot; + } else if ( + typeof junitReporterOptions.outputDir === 'string' && + !path.isAbsolute(junitReporterOptions.outputDir) + ) { + junitReporterOptions.outputDir = path.join( + context.workspaceRoot, + junitReporterOptions.outputDir, + ); + } + } else { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (parsedKarmaConfig as any)['junitReporter'] = { + outputDir: context.workspaceRoot, + }; + } + } + // When using code-coverage, auto-add karma-coverage. // This was done as part of the karma plugin for webpack. if ( diff --git a/tests/legacy-cli/e2e/tests/test/karma-junit-output.ts b/tests/legacy-cli/e2e/tests/test/karma-junit-output.ts new file mode 100644 index 000000000000..056adea26ab3 --- /dev/null +++ b/tests/legacy-cli/e2e/tests/test/karma-junit-output.ts @@ -0,0 +1,27 @@ +import { expectFileMatchToExist, replaceInFile } from '../../utils/fs'; +import { installPackage } from '../../utils/packages'; +import { silentNg } from '../../utils/process'; + +const E2E_CUSTOM_LAUNCHER = ` + customLaunchers: { + ChromeHeadlessNoSandbox: { + base: 'ChromeHeadless', + flags: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-dev-shm-usage'], + }, + }, + restartOnFileChange: true, +`; + +export default async function () { + await installPackage('karma-junit-reporter'); + await silentNg('generate', 'config', 'karma'); + + await replaceInFile('karma.conf.js', 'karma-jasmine-html-reporter', 'karma-junit-reporter'); + await replaceInFile('karma.conf.js', `'kjhtml'`, `'junit'`); + + await replaceInFile('karma.conf.js', `restartOnFileChange: true`, E2E_CUSTOM_LAUNCHER); + + await silentNg('test', '--no-watch'); + + await expectFileMatchToExist('.', /TESTS\-.+\.xml/); +}