feat(list-reporter): Add appendTags option

This commit is contained in:
Mark Stewart 2024-10-02 15:06:14 +01:00
parent 3a5bf1cc1d
commit db925359d9
No known key found for this signature in database
GPG key ID: 8F4C93FFAE542241
4 changed files with 26 additions and 8 deletions

View file

@ -102,6 +102,7 @@ List report supports the following configuration options and environment variabl
| Environment Variable Name | Reporter Config Option| Description | Default | Environment Variable Name | Reporter Config Option| Description | Default
|---|---|---|---| |---|---|---|---|
| `PLAYWRIGHT_LIST_PRINT_STEPS` | `printSteps` | Whether to print each step on its own line. | `false` | `PLAYWRIGHT_LIST_PRINT_STEPS` | `printSteps` | Whether to print each step on its own line. | `false`
| `PLAYWRIGHT_LIST_APPEND_TAGS` | `appendTags` | Whether to append tags to the end of the test name. | `false`
| `PLAYWRIGHT_FORCE_TTY` | | Whether to produce output suitable for a live terminal. If a number is specified, it will also be used as the terminal width. | `true` when terminal is in TTY mode, `false` otherwise. | `PLAYWRIGHT_FORCE_TTY` | | Whether to produce output suitable for a live terminal. If a number is specified, it will also be used as the terminal width. | `true` when terminal is in TTY mode, `false` otherwise.
| `FORCE_COLOR` | | Whether to produce colored output. | `true` when terminal is in TTY mode, `false` otherwise. | `FORCE_COLOR` | | Whether to produce colored output. | `true` when terminal is in TTY mode, `false` otherwise.

View file

@ -401,7 +401,7 @@ export function stepSuffix(step: TestStep | undefined) {
return stepTitles.map(t => t.split('\n')[0]).map(t => ' ' + t).join(''); return stepTitles.map(t => t.split('\n')[0]).map(t => ' ' + t).join('');
} }
export function formatTestTitle(config: FullConfig, test: TestCase, step?: TestStep, omitLocation: boolean = false): string { export function formatTestTitle(config: FullConfig, test: TestCase, step?: TestStep, omitLocation: boolean = false, appendTags: boolean = false): string {
// root, project, file, ...describes, test // root, project, file, ...describes, test
const [, projectName, , ...titles] = test.titlePath(); const [, projectName, , ...titles] = test.titlePath();
let location; let location;
@ -410,7 +410,8 @@ export function formatTestTitle(config: FullConfig, test: TestCase, step?: TestS
else else
location = `${relativeTestPath(config, test)}:${step?.location?.line ?? test.location.line}:${step?.location?.column ?? test.location.column}`; location = `${relativeTestPath(config, test)}:${step?.location?.line ?? test.location.line}:${step?.location?.column ?? test.location.column}`;
const projectTitle = projectName ? `[${projectName}] ` : ''; const projectTitle = projectName ? `[${projectName}] ` : '';
return `${projectTitle}${location} ${titles.join(' ')}${stepSuffix(step)}`; const tags = appendTags && test.tags.length > 0 ? ` [${test.tags.join(', ')}]` : '';
return `${projectTitle}${location} ${titles.join(' ')}${stepSuffix(step)}${tags}`;
} }
function formatTestHeader(config: FullConfig, test: TestCase, options: { indent?: string, index?: number, mode?: 'default' | 'error' } = {}): string { function formatTestHeader(config: FullConfig, test: TestCase, options: { indent?: string, index?: number, mode?: 'default' | 'error' } = {}): string {

View file

@ -33,10 +33,12 @@ class ListReporter extends BaseReporter {
private _stepIndex = new Map<TestStep, string>(); private _stepIndex = new Map<TestStep, string>();
private _needNewLine = false; private _needNewLine = false;
private _printSteps: boolean; private _printSteps: boolean;
private _appendTags: boolean;
constructor(options: { printSteps?: boolean } = {}) { constructor(options: { printSteps?: boolean; appendTags?: boolean } = {}) {
super(); super();
this._printSteps = getAsBooleanFromENV('PLAYWRIGHT_LIST_PRINT_STEPS', options.printSteps); this._printSteps = getAsBooleanFromENV('PLAYWRIGHT_LIST_PRINT_STEPS', options.printSteps);
this._appendTags = getAsBooleanFromENV('PLAYWRIGHT_LIST_APPEND_TAGS', options.appendTags);
} }
override onBegin(suite: Suite) { override onBegin(suite: Suite) {
@ -57,7 +59,7 @@ class ListReporter extends BaseReporter {
this._maybeWriteNewLine(); this._maybeWriteNewLine();
this._testRows.set(test, this._lastRow); this._testRows.set(test, this._lastRow);
const prefix = this._testPrefix(index, ''); const prefix = this._testPrefix(index, '');
const line = colors.dim(formatTestTitle(this.config, test)) + this._retrySuffix(result); const line = colors.dim(formatTestTitle(this.config, test, undefined, false, this._appendTags)) + this._retrySuffix(result);
this._appendLine(line, prefix); this._appendLine(line, prefix);
} }
@ -97,7 +99,7 @@ class ListReporter extends BaseReporter {
const line = test.title + colors.dim(stepSuffix(step)); const line = test.title + colors.dim(stepSuffix(step));
this._appendLine(line, prefix); this._appendLine(line, prefix);
} else { } else {
this._updateLine(this._testRows.get(test)!, colors.dim(formatTestTitle(this.config, test, step)) + this._retrySuffix(result), this._testPrefix(testIndex, '')); this._updateLine(this._testRows.get(test)!, colors.dim(formatTestTitle(this.config, test, step, false, this._appendTags)) + this._retrySuffix(result), this._testPrefix(testIndex, ''));
} }
} }
@ -108,12 +110,12 @@ class ListReporter extends BaseReporter {
const testIndex = this._resultIndex.get(result) || ''; const testIndex = this._resultIndex.get(result) || '';
if (!this._printSteps) { if (!this._printSteps) {
if (isTTY) if (isTTY)
this._updateLine(this._testRows.get(test)!, colors.dim(formatTestTitle(this.config, test, step.parent)) + this._retrySuffix(result), this._testPrefix(testIndex, '')); this._updateLine(this._testRows.get(test)!, colors.dim(formatTestTitle(this.config, test, step.parent, false, this._appendTags)) + this._retrySuffix(result), this._testPrefix(testIndex, ''));
return; return;
} }
const index = this.getStepIndex(testIndex, result, step); const index = this.getStepIndex(testIndex, result, step);
const title = isTTY ? test.title + colors.dim(stepSuffix(step)) : formatTestTitle(this.config, test, step); const title = isTTY ? test.title + colors.dim(stepSuffix(step)) : formatTestTitle(this.config, test, step, false, this._appendTags);
const prefix = this._testPrefix(index, ''); const prefix = this._testPrefix(index, '');
let text = ''; let text = '';
if (step.error) if (step.error)
@ -161,7 +163,7 @@ class ListReporter extends BaseReporter {
override onTestEnd(test: TestCase, result: TestResult) { override onTestEnd(test: TestCase, result: TestResult) {
super.onTestEnd(test, result); super.onTestEnd(test, result);
const title = formatTestTitle(this.config, test); const title = formatTestTitle(this.config, test, undefined, false, this._appendTags);
let prefix = ''; let prefix = '';
let text = ''; let text = '';

View file

@ -258,6 +258,20 @@ for (const useIntermediateMergeReport of [false, true] as const) {
expect(text).toContain('1) a.test.ts:3:15 passes outer 1.0 inner 1.1 ──'); expect(text).toContain('1) a.test.ts:3:15 passes outer 1.0 inner 1.1 ──');
expect(result.exitCode).toBe(1); expect(result.exitCode).toBe(1);
}); });
test('append tags', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.ts': `
const { test, expect } = require('@playwright/test');
test('passes', { tag: ['@foo', '@bar'] }, async ({}) => {
expect(0).toBe(0);
});
`,
}, { reporter: 'list', workers: '1' }, { PLAYWRIGHT_LIST_APPEND_TAGS: '1' });
const text = result.output;
expect(text).toContain(`${POSITIVE_STATUS_MARK} 1 a.test.ts:3:11 passes [@foo, @bar]`);
});
}); });
} }