From ae489b1c43e79adceacb0ce9035558110e649690 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 8 Jul 2021 18:24:07 +0200 Subject: [PATCH] fix(test-runner): do not override error with unhandled error (#7507) --- src/test/workerRunner.ts | 2 ++ tests/playwright-test/runner.spec.ts | 29 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/test/workerRunner.ts b/src/test/workerRunner.ts index 6ab9735717..566dfac71f 100644 --- a/src/test/workerRunner.ts +++ b/src/test/workerRunner.ts @@ -72,6 +72,8 @@ export class WorkerRunner extends EventEmitter { if (this._isStopped) return; if (this._currentTest) { + if (this._currentTest.testInfo.error) + return; this._currentTest.testInfo.status = 'failed'; this._currentTest.testInfo.error = serializeError(error); this._failedTestId = this._currentTest.testId; diff --git a/tests/playwright-test/runner.spec.ts b/tests/playwright-test/runner.spec.ts index ba27d9e5c4..21e59672c9 100644 --- a/tests/playwright-test/runner.spec.ts +++ b/tests/playwright-test/runner.spec.ts @@ -110,3 +110,32 @@ test('sigint should stop workers', async ({ runInlineTest }) => { expect(result.output).not.toContain('%%skipped1'); expect(result.output).not.toContain('%%skipped2'); }); + +test('should use the first occurring error when an unhandled exception was thrown', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'unhandled-exception.spec.js': ` + const test = pwt.test.extend({ + context: async ({}, test) => { + await test(123) + let errorWasThrownPromiseResolve = () => {} + const errorWasThrownPromise = new Promise(resolve => errorWasThrownPromiseResolve = resolve); + setTimeout(() => { + errorWasThrownPromiseResolve(); + throw new Error('second error'); + }, 0) + await errorWasThrownPromise; + }, + page: async ({ context}, test) => { + throw new Error('first error'); + await test(123) + }, + }); + + test('my-test', async ({ page }) => { }); + ` + }); + expect(result.exitCode).toBe(1); + expect(result.passed).toBe(0); + expect(result.failed).toBe(1); + expect(result.report.suites[0].specs[0].tests[0].results[0].error.message).toBe('first error'); +});