From e638c4597f84342b47e83d7e9ff4bd2c8db178eb Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 9 Aug 2021 14:21:53 -0700 Subject: [PATCH] fix(test runner): do not swallow afterAll failure (#8099) --- src/test/dispatcher.ts | 6 ++++++ tests/playwright-test/hooks.spec.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/test/dispatcher.ts b/src/test/dispatcher.ts index 7d59a987b8..3a4575717f 100644 --- a/src/test/dispatcher.ts +++ b/src/test/dispatcher.ts @@ -144,6 +144,12 @@ export class Dispatcher { failedTestIds.add(test._id); first = false; } + if (first) { + // We had a fatal error after all tests have passed - most likely in the afterAll hook. + // Let's just fail the test run. + this._hasWorkerErrors = true; + this._reporter.onError?.(params.fatalError); + } // Since we pretend that all remaining tests failed, there is nothing else to run, // except for possible retries. remaining = []; diff --git a/tests/playwright-test/hooks.spec.ts b/tests/playwright-test/hooks.spec.ts index 18bb26be72..d8bae155f6 100644 --- a/tests/playwright-test/hooks.spec.ts +++ b/tests/playwright-test/hooks.spec.ts @@ -242,3 +242,19 @@ test('beforeAll hook should get retry index of the first test', async ({ runInli '%%test-retry-1', ]); }); + +test('afterAll exception should fail the run', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.js': ` + const { test } = pwt; + test.afterAll(() => { + throw new Error('From the afterAll'); + }); + test('passed', () => { + }); + `, + }); + expect(result.exitCode).toBe(1); + expect(result.passed).toBe(1); + expect(result.output).toContain('From the afterAll'); +});