fix(test runner): do not mask uncaught error in beforeEach (#9764)

This commit is contained in:
Dmitry Gozman 2021-10-25 14:17:27 -07:00 committed by GitHub
parent ada7f4be23
commit 13ed1dee50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View file

@ -435,8 +435,12 @@ export class WorkerRunner extends EventEmitter {
if (testInfo.status === 'passed')
testInfo.status = 'skipped';
} else {
testInfo.status = 'failed';
testInfo.error = serializeError(error);
if (testInfo.status === 'passed')
testInfo.status = 'failed';
// Do not overwrite any uncaught error that happened first.
// This is typical if you have some expect() that fails in beforeEach.
if (!('error' in testInfo))
testInfo.error = serializeError(error);
}
// Continue running afterEach hooks even after the failure.
}

View file

@ -14,7 +14,7 @@
* limitations under the License.
*/
import { test, expect } from './playwright-test-fixtures';
import { test, expect, stripAscii } from './playwright-test-fixtures';
test('hooks should work with fixtures', async ({ runInlineTest }) => {
const { results } = await runInlineTest({
@ -517,3 +517,29 @@ test('afterEach should get the test status right away', async ({ runInlineTest }
'%%timing out: timedOut',
]);
});
test('uncaught error in beforeEach should not be masked by another error', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.js': `
const test = pwt.test.extend({
foo: async ({}, use) => {
let cb;
await use(new Promise((f, r) => cb = r));
cb(new Error('Oh my!'));
},
});
test.beforeEach(async ({ foo }, testInfo) => {
setTimeout(() => {
expect(1).toBe(2);
}, 0);
await foo;
});
test('passing', () => {
});
`,
});
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(stripAscii(result.output)).toContain('Expected: 2');
expect(stripAscii(result.output)).toContain('Received: 1');
});