Skip to content

[Feature] Show Custom expect message in case of failure v2 #35012

@I-Iege

Description

@I-Iege

🚀 Feature Request

In the documentation it is mentioned to have possibility to add custom expect message
await expect(page.getByText('Name'), 'should be logged in').toBeVisible();

This message will be shown in reporters, both for passing and failing expects, providing more context about the assertion.

When expect passes, you might see a successful step like this:

✅ should be logged in @example.spec.ts:18

But it is mostly meaningful in case of failure when we can not see the message.
There are hacky solutions to create a hook around expect, but not easy to use and not works in every environment.

Example

What i want is to have possibility to change assert message. Lets say i have a test case with a string where i call findIndex
expect(line.findIndex("string to find")).toBeGreaterThan(-1)
in this case we have a bad error message like :
Expected: > -1
Received: -1

there is a possibility to add message to expect
expect(line.findIndex("string to find"), "String not found!" ).toBeGreaterThan(-1)
but it is ignored in test execution i got the same message if test fail:
Expected: > -1
Received: -1

instead of that would be good to see my message from except in case of test fail:
"String not found!"

or both information in the report:
"String not found!"
Expected: > -1
Received: -1

Motivation

I would like to make test results easier to analyze.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions