fix(test runner): expect.poll error reporting should handle non-expect errors

This commit is contained in:
Simon Knott 2024-08-22 08:44:50 +02:00
parent 571f25a7d3
commit 8ff2fc886c
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 21 additions and 3 deletions

View file

@ -60,7 +60,7 @@ import {
} from '../common/expectBundle';
import { zones } from 'playwright-core/lib/utils';
import { TestInfoImpl } from '../worker/testInfo';
import { ExpectError } from './matcherHint';
import { ExpectError, isExpectError } from './matcherHint';
// #region
// Mirrored from https://github.com/facebook/jest/blob/f13abff8df9a0e1148baf3584bcde6d1b479edc7/packages/expect/src/print.ts
@ -289,8 +289,9 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
const step = testInfo._addStep(stepInfo);
const reportStepError = (jestError: ExpectError) => {
const error = new ExpectError(jestError, customMessage, stackFrames);
const reportStepError = (_error: unknown) => {
const jestError = _error instanceof Error ? _error : new Error(String(_error));
const error = isExpectError(jestError) ? new ExpectError(jestError, customMessage, stackFrames) : jestError;
step.complete({ error });
if (this._info.isSoft)
testInfo._failWithError(error);

View file

@ -64,3 +64,7 @@ export class ExpectError extends Error {
this.stack = this.name + ': ' + this.message + '\n' + stringifyStackFrames(stackFrames).join('\n');
}
}
export function isExpectError(e: unknown): e is ExpectError {
return e instanceof Error && 'matcherResult' in e;
}

View file

@ -232,3 +232,16 @@ test('should show intermediate result for poll that spills over test time', asyn
expect(result.output).toContain('Expected: 2');
expect(result.output).toContain('Received: 3');
});
test('should propagate promise rejections', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/32256' } }, async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
test('should fail', async () => {
await expect.poll(() => Promise.reject('some error')).toBe({ foo: 'baz' });
});
`
});
expect(result.output).toContain('Error: some error');
});