Skip to content

Commit 66410bc

Browse files
authored
feat(junit): add addFileAttribute and fileRoot options (#44107)
1 parent b5b158a commit 66410bc

5 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎docs/src/test-reporters-js.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ JUnit report supports following configuration options and environment variables:
482482
| `PLAYWRIGHT_JUNIT_STRIP_ANSI` | `stripANSIControlSequences` | Whether to remove ANSI control sequences from the text before writing it in the report. | By default output text is added as is.
483483
| `PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAME` | `includeProjectInTestName` | Whether to include Playwright project name in every test case as a name prefix. | By default not included.
484484
| `PLAYWRIGHT_JUNIT_OMIT_TAGS` | `omitTags` | Whether to omit test tags that are automatically appended to failure details. | `false`
485+
| `PLAYWRIGHT_JUNIT_ADD_FILE_ATTRIBUTE` | `addFileAttribute` | Whether to add a `file` attribute to every test case, with the path of the test file relative to `fileRoot`. | `false`
486+
| `PLAYWRIGHT_JUNIT_FILE_ROOT` | `fileRoot` | Directory that the `file` attribute is relative to, for example the repository root in a monorepo. The option is resolved relative to the config directory, the environment variable relative to `cwd`. | Config directory.
485487
| `PLAYWRIGHT_JUNIT_SUITE_ID` | | Value of the `id` attribute on the root `<testsuites/>` report entry. | Empty string.
486488
| `PLAYWRIGHT_JUNIT_SUITE_NAME` | | Value of the `name` attribute on the root `<testsuites/>` report entry. | Empty string.
487489

‎packages/playwright/src/reporters/junit.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import fs from 'fs';
1818
import path from 'path';
1919

2020
import { getAsBooleanFromENV } from '@utils/env';
21+
import { toPosixPath } from '@utils/fileUtils';
2122

2223
import { createTestFilter, filterTests, formatFailure, nonTerminalScreen, resolveOutputFile, visitTests } from './base';
2324
import { stripAnsiEscapes } from '../util';
@@ -41,6 +42,8 @@ class JUnitReporter implements ReporterV2 {
4142
private includeProjectInTestName = false;
4243
private includeRetries = false;
4344
private omitTags = false;
45+
private addFileAttribute = false;
46+
private fileRoot: string;
4447
private testFilter: TestFilter | undefined;
4548

4649
constructor(options: JUnitReporterOptions & CommonReporterOptions) {
@@ -49,6 +52,9 @@ class JUnitReporter implements ReporterV2 {
4952
this.includeProjectInTestName = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAME', !!options.includeProjectInTestName);
5053
this.includeRetries = !!this.testFilter || getAsBooleanFromENV('PLAYWRIGHT_JUNIT_INCLUDE_RETRIES', !!options.includeRetries);
5154
this.omitTags = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_OMIT_TAGS', !!options.omitTags);
55+
this.addFileAttribute = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_ADD_FILE_ATTRIBUTE', !!options.addFileAttribute);
56+
const fileRootEnv = process.env.PLAYWRIGHT_JUNIT_FILE_ROOT;
57+
this.fileRoot = fileRootEnv ? path.resolve(process.cwd(), fileRootEnv) : path.resolve(options.configDir, options.fileRoot || '');
5258
this.configDir = options.configDir;
5359
this.resolvedOutputFile = resolveOutputFile('JUNIT', options)?.outputFile;
5460
}
@@ -160,6 +166,8 @@ class JUnitReporter implements ReporterV2 {
160166
},
161167
children: [],
162168
};
169+
if (this.addFileAttribute)
170+
entry.attributes!.file = toPosixPath(path.relative(this.fileRoot, test.location.file));
163171
entries.push(entry);
164172

165173
// Xray Test Management supports testcase level properties, where additional metadata may be provided

‎packages/playwright/types/test.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type FailuresReporterOptions = { omitTags?: boolean };
2424
export type LineReporterOptions = { omitTags?: boolean };
2525
export type ListReporterOptions = { printSteps?: boolean, printFailuresInline?: boolean, omitTags?: boolean };
2626
export type GitHubReporterOptions = { omitTags?: boolean };
27-
export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean };
27+
export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean, addFileAttribute?: boolean, fileRoot?: string };
2828
export type JsonReporterOptions = { outputFile?: string, onlyFailures?: boolean };
2929
export type PerfettoReporterOptions = { outputFile?: string };
3030
export type HtmlReporterOptions = {

‎tests/playwright-test/reporter-junit.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,42 @@ for (const useIntermediateMergeReport of [false, true] as const) {
347347
expect(xml['testsuites']['testsuite'][1]['testcase'][0]['$']['name']).toBe('[project2] one');
348348
});
349349

350+
test('should add file attribute relative to fileRoot', {
351+
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/44099' },
352+
}, async ({ runInlineTest }) => {
353+
const files = {
354+
'packages/foo/playwright.config.ts': `
355+
module.exports = {
356+
testDir: './src',
357+
reporter: [['junit', { addFileAttribute: true, fileRoot: process.env.TEST_FILE_ROOT }]]
358+
};
359+
`,
360+
'packages/foo/src/auth/a.test.js': `
361+
import { test, expect } from '@playwright/test';
362+
test('one', async ({}) => {
363+
expect(1).toBe(1);
364+
});
365+
`,
366+
};
367+
const run = async (env: Record<string, string>) => {
368+
const result = await runInlineTest(files, { reporter: '', config: './packages/foo/playwright.config.ts' }, env);
369+
expect(result.exitCode).toBe(0);
370+
return parseXML(result.output)['testsuites']['testsuite'][0]['testcase'][0]['$'];
371+
};
372+
373+
const byDefault = await run({});
374+
expect(byDefault['classname']).toBe('auth/a.test.js');
375+
expect(byDefault['file']).toBe('src/auth/a.test.js');
376+
377+
// The option is relative to the config directory.
378+
expect((await run({ TEST_FILE_ROOT: '../..' }))['file']).toBe('packages/foo/src/auth/a.test.js');
379+
380+
// The environment variable wins and is relative to cwd.
381+
expect((await run({ TEST_FILE_ROOT: '../..', PLAYWRIGHT_JUNIT_FILE_ROOT: 'packages' }))['file']).toBe('foo/src/auth/a.test.js');
382+
383+
expect((await run({ PLAYWRIGHT_JUNIT_ADD_FILE_ATTRIBUTE: '0' }))['file']).toBeUndefined();
384+
});
385+
350386
test('should render existing attachments, but not missing ones', async ({ runInlineTest }) => {
351387
test.skip(useIntermediateMergeReport, 'Blob report hashes attachment paths');
352388
const result = await runInlineTest({

‎utils/generate_types/overrides-test.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type FailuresReporterOptions = { omitTags?: boolean };
2323
export type LineReporterOptions = { omitTags?: boolean };
2424
export type ListReporterOptions = { printSteps?: boolean, printFailuresInline?: boolean, omitTags?: boolean };
2525
export type GitHubReporterOptions = { omitTags?: boolean };
26-
export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean };
26+
export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean, addFileAttribute?: boolean, fileRoot?: string };
2727
export type JsonReporterOptions = { outputFile?: string, onlyFailures?: boolean };
2828
export type PerfettoReporterOptions = { outputFile?: string };
2929
export type HtmlReporterOptions = {

0 commit comments

Comments
 (0)