fix(test-fail): allow unhandled expects in test.fail (#11850)

Previously, we would consider this a worker error, but
we make an exception for "expect()" calls.
This commit is contained in:
Dmitry Gozman 2022-02-03 17:14:12 -08:00 committed by GitHub
parent 72424dc904
commit d9a8bb057d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -94,7 +94,11 @@ export class WorkerRunner extends EventEmitter {
// a test runner. In the latter case, the worker state could be messed up,
// and continuing to run tests in the same worker is problematic. Therefore,
// we turn this into a fatal error and restart the worker anyway.
if (this._currentTest && this._currentTest._test._type === 'test' && this._currentTest.expectedStatus !== 'failed') {
// The only exception is the expect() error that we still consider ok.
const isExpectError = (error instanceof Error) && !!(error as any).matcherResult;
const isCurrentTestExpectedToFail = this._currentTest?.expectedStatus === 'failed';
const shouldConsiderAsTestError = isExpectError || !isCurrentTestExpectedToFail;
if (this._currentTest && this._currentTest._test._type === 'test' && shouldConsiderAsTestError) {
this._currentTest._failWithError(serializeError(error), true /* isHardError */);
} else {
// No current test - fatal error.

View File

@ -426,3 +426,19 @@ test('should not reuse worker after unhandled rejection in test.fail', async ({
expect(result.output).toContain(`Error: Oh my!`);
expect(result.output).not.toContain(`Did not teardown test scope`);
});
test('should allow unhandled expects in test.fail', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.spec.ts': `
const { test } = pwt;
test('failing1', async ({}) => {
test.fail();
Promise.resolve().then(() => expect(1).toBe(2));
await new Promise(f => setTimeout(f, 100));
});
`
});
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
expect(result.output).not.toContain(`Error: expect`);
});