From db925359d9999dd469e6cfba2bc10edbaacb8794 Mon Sep 17 00:00:00 2001 From: Mark Stewart Date: Wed, 2 Oct 2024 15:06:14 +0100 Subject: [PATCH] feat(list-reporter): Add appendTags option --- docs/src/test-reporters-js.md | 1 + packages/playwright/src/reporters/base.ts | 5 +++-- packages/playwright/src/reporters/list.ts | 14 ++++++++------ tests/playwright-test/reporter-list.spec.ts | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/docs/src/test-reporters-js.md b/docs/src/test-reporters-js.md index 952d74b923..aaf80d97af 100644 --- a/docs/src/test-reporters-js.md +++ b/docs/src/test-reporters-js.md @@ -102,6 +102,7 @@ List report supports the following configuration options and environment variabl | 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_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. | `FORCE_COLOR` | | Whether to produce colored output. | `true` when terminal is in TTY mode, `false` otherwise. diff --git a/packages/playwright/src/reporters/base.ts b/packages/playwright/src/reporters/base.ts index 6776ce606f..433228d7e6 100644 --- a/packages/playwright/src/reporters/base.ts +++ b/packages/playwright/src/reporters/base.ts @@ -401,7 +401,7 @@ export function stepSuffix(step: TestStep | undefined) { 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 const [, projectName, , ...titles] = test.titlePath(); let location; @@ -410,7 +410,8 @@ export function formatTestTitle(config: FullConfig, test: TestCase, step?: TestS else location = `${relativeTestPath(config, test)}:${step?.location?.line ?? test.location.line}:${step?.location?.column ?? test.location.column}`; 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 { diff --git a/packages/playwright/src/reporters/list.ts b/packages/playwright/src/reporters/list.ts index 8704bfe104..fe1518e585 100644 --- a/packages/playwright/src/reporters/list.ts +++ b/packages/playwright/src/reporters/list.ts @@ -33,10 +33,12 @@ class ListReporter extends BaseReporter { private _stepIndex = new Map(); private _needNewLine = false; private _printSteps: boolean; + private _appendTags: boolean; - constructor(options: { printSteps?: boolean } = {}) { + constructor(options: { printSteps?: boolean; appendTags?: boolean } = {}) { super(); this._printSteps = getAsBooleanFromENV('PLAYWRIGHT_LIST_PRINT_STEPS', options.printSteps); + this._appendTags = getAsBooleanFromENV('PLAYWRIGHT_LIST_APPEND_TAGS', options.appendTags); } override onBegin(suite: Suite) { @@ -57,7 +59,7 @@ class ListReporter extends BaseReporter { this._maybeWriteNewLine(); this._testRows.set(test, this._lastRow); 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); } @@ -97,7 +99,7 @@ class ListReporter extends BaseReporter { const line = test.title + colors.dim(stepSuffix(step)); this._appendLine(line, prefix); } 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) || ''; if (!this._printSteps) { 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; } 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, ''); let text = ''; if (step.error) @@ -161,7 +163,7 @@ class ListReporter extends BaseReporter { override onTestEnd(test: TestCase, result: TestResult) { super.onTestEnd(test, result); - const title = formatTestTitle(this.config, test); + const title = formatTestTitle(this.config, test, undefined, false, this._appendTags); let prefix = ''; let text = ''; diff --git a/tests/playwright-test/reporter-list.spec.ts b/tests/playwright-test/reporter-list.spec.ts index c57190a5d7..c8ed723f4f 100644 --- a/tests/playwright-test/reporter-list.spec.ts +++ b/tests/playwright-test/reporter-list.spec.ts @@ -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(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]`); + }); }); }