From 9ae77a71fd6843a8c6a9a559fd0c5043690ba76d Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Fri, 25 Aug 2023 19:06:49 +0200 Subject: [PATCH] feat: print message if maxFailures has reached (#26322) Fixes https://github.com/microsoft/playwright/issues/24239 --- .../playwright-test/src/reporters/base.ts | 10 +++++++ tests/playwright-test/reporter-base.spec.ts | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/playwright-test/src/reporters/base.ts b/packages/playwright-test/src/reporters/base.ts index 12d1d0fe33..33e384f10c 100644 --- a/packages/playwright-test/src/reporters/base.ts +++ b/packages/playwright-test/src/reporters/base.ts @@ -55,6 +55,7 @@ export class BaseReporter implements ReporterV2 { private _omitFailures: boolean; private readonly _ttyWidthForTest: number; private _fatalErrors: TestError[] = []; + private _failureCount: number = 0; constructor(options: { omitFailures?: boolean } = {}) { this._omitFailures = options.omitFailures || false; @@ -94,6 +95,8 @@ export class BaseReporter implements ReporterV2 { } onTestEnd(test: TestCase, result: TestResult) { + if (result.status !== 'skipped' && result.status !== test.expectedStatus) + ++this._failureCount; // Ignore any tests that are run in parallel. for (let suite: Suite | undefined = test.parent; suite; suite = suite.parent) { if ((suite as SuitePrivate)._parallelMode === 'parallel') @@ -232,6 +235,7 @@ export class BaseReporter implements ReporterV2 { if (full && summary.failuresToPrint.length && !this._omitFailures) this._printFailures(summary.failuresToPrint); this._printSlowTests(); + this._printMaxFailuresReached(); this._printSummary(summaryMessage); } @@ -253,6 +257,12 @@ export class BaseReporter implements ReporterV2 { console.log(colors.yellow(' Consider splitting slow test files to speed up parallel execution')); } + private _printMaxFailuresReached() { + if (this.config.maxFailures && this._failureCount < this.config.maxFailures) + return; + console.log(colors.yellow(`Testing stopped early after ${this.config.maxFailures} maximum allowed failures.`)); + } + private _printSummary(summary: string) { if (summary.trim()) console.log(summary); diff --git a/tests/playwright-test/reporter-base.spec.ts b/tests/playwright-test/reporter-base.spec.ts index 494180ec53..7b0c5a40a4 100644 --- a/tests/playwright-test/reporter-base.spec.ts +++ b/tests/playwright-test/reporter-base.spec.ts @@ -182,6 +182,33 @@ for (const useIntermediateMergeReport of [false, true] as const) { expect(result.output).not.toContain(`Slow test file: [qux] › dir${path.sep}b.test.js (`); }); + test('should print if maxFailures is reached', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { + maxFailures: 1, + }; + `, + 'dir/a.test.js': ` + import { test, expect } from '@playwright/test'; + test('failing1', async ({}) => { + expect(1).toBe(2); + }); + test('failing2', async ({}) => { + expect(1).toBe(2); + }); + test('failing3', async ({}) => { + expect(1).toBe(2); + }); + `, + }); + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(result.passed).toBe(0); + expect(result.skipped).toBe(2); + expect(result.output).toContain('Testing stopped early after 1 maximum allowed failures.'); + }); + test('should not print slow parallel tests', async ({ runInlineTest }) => { const result = await runInlineTest({ 'playwright.config.ts': `