fix(expect): mark step as failed when async custom matcher throws (#23035)

Fixes #23021.
This commit is contained in:
Dmitry Gozman 2023-05-15 19:37:12 -07:00 committed by GitHub
parent bf1df9678f
commit f469e4b1eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -312,6 +312,8 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
} else {
try {
const result = matcher.call(target, ...args);
if (result instanceof Promise)
return result.then(finalizer).catch(reportStepError);
finalizer();
return result;
} catch (e) {

View File

@ -480,7 +480,7 @@ test('should report custom expect steps', async ({ runInlineTest }) => {
'reporter.ts': stepHierarchyReporter,
'playwright.config.ts': `
module.exports = {
reporter: './reporter',
reporter: [['./reporter'], ['line']],
};
`,
'a.test.ts': `
@ -501,16 +501,27 @@ test('should report custom expect steps', async ({ runInlineTest }) => {
};
}
},
async toBeFailingAsync(received) {
await new Promise(f => setTimeout(f, 0));
return {
message: () => "It fails!",
pass: false,
};
},
});
import { test, expect } from '@playwright/test';
test('pass', async ({}) => {
test('fail', async ({}) => {
expect(15).toBeWithinRange(10, 20);
await expect(1).toBeFailingAsync(22);
});
`
}, { reporter: '', workers: 1 });
expect(result.exitCode).toBe(0);
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('It fails!');
const objects = result.outputLines.map(line => JSON.parse(line));
expect(objects).toEqual([
{
@ -526,6 +537,16 @@ test('should report custom expect steps', async ({ runInlineTest }) => {
},
title: 'expect.toBeWithinRange',
},
{
category: 'expect',
location: {
column: 'number',
file: 'a.test.ts',
line: 'number',
},
title: 'expect.toBeFailingAsync',
error: '<error>',
},
{
category: 'hook',
title: 'After Hooks',