Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test-runner): do not override error with unhandled error (#7507)

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