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'); +});