Skip to content

Commit 5814393

Browse files
committed
fix(@angular/build): resolve junit karma reporter output to workspace root
To maintain behavior with the Webpack-based karma unit-testing available via `@angular-devkit/build-angular`, the application build system based karma testing will now resolve the output directory location of the junit karma reporter to the same workspace root location. This is only performed if the `junit` reporter is enabled and the reporter's `outputDir` option is not an absolute path. (cherry picked from commit bec42f7)
1 parent 83c820e commit 5814393

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,35 @@ async function initializeApplication(
577577
parsedKarmaConfig.reporters ??= [];
578578
parsedKarmaConfig.reporters.push(AngularPolyfillsPlugin.NAME);
579579

580+
// Adjust karma junit reporter outDir location to maintain previous (devkit) behavior
581+
// The base path for the reporter was previously the workspace root.
582+
// To keep the files in the same location, the reporter's output directory is adjusted
583+
// to be relative to the workspace root when using junit.
584+
if (parsedKarmaConfig.reporters?.some((reporter) => reporter === 'junit')) {
585+
if ('junitReporter' in parsedKarmaConfig) {
586+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
587+
const junitReporterOptions = (parsedKarmaConfig as any)['junitReporter'] as {
588+
outputDir?: unknown;
589+
};
590+
if (junitReporterOptions.outputDir == undefined) {
591+
junitReporterOptions.outputDir = context.workspaceRoot;
592+
} else if (
593+
typeof junitReporterOptions.outputDir === 'string' &&
594+
!path.isAbsolute(junitReporterOptions.outputDir)
595+
) {
596+
junitReporterOptions.outputDir = path.join(
597+
context.workspaceRoot,
598+
junitReporterOptions.outputDir,
599+
);
600+
}
601+
} else {
602+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
603+
(parsedKarmaConfig as any)['junitReporter'] = {
604+
outputDir: context.workspaceRoot,
605+
};
606+
}
607+
}
608+
580609
// When using code-coverage, auto-add karma-coverage.
581610
// This was done as part of the karma plugin for webpack.
582611
if (
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expectFileMatchToExist, replaceInFile } from '../../utils/fs';
2+
import { installPackage } from '../../utils/packages';
3+
import { silentNg } from '../../utils/process';
4+
5+
const E2E_CUSTOM_LAUNCHER = `
6+
customLaunchers: {
7+
ChromeHeadlessNoSandbox: {
8+
base: 'ChromeHeadless',
9+
flags: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-dev-shm-usage'],
10+
},
11+
},
12+
restartOnFileChange: true,
13+
`;
14+
15+
export default async function () {
16+
await installPackage('karma-junit-reporter');
17+
await silentNg('generate', 'config', 'karma');
18+
19+
await replaceInFile('karma.conf.js', 'karma-jasmine-html-reporter', 'karma-junit-reporter');
20+
await replaceInFile('karma.conf.js', `'kjhtml'`, `'junit'`);
21+
22+
await replaceInFile('karma.conf.js', `restartOnFileChange: true`, E2E_CUSTOM_LAUNCHER);
23+
24+
await silentNg('test', '--no-watch');
25+
26+
await expectFileMatchToExist('.', /TESTS\-.+\.xml/);
27+
}

0 commit comments

Comments
 (0)