Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: toThrow reports returned value for non-throw completions #15525

Open
blended-bram opened this issue Feb 26, 2025 · 0 comments
Open

Comments

@blended-bram
Copy link

blended-bram commented Feb 26, 2025

🚀 Feature Proposal

When an assertion for .toThrow() fails because the function did not throw an error, report the returned value instead.

Motivation

Currently when a toThrow assertion fails because of a normal return, it is reported as:

Error: expect(received).toThrow()

Received function did not throw

And when given a matcher (in this case expect.any(String)):

Error: expect(received).toThrow(expected)

Expected asymmetric matcher: Any<String>

Received function did not throw

Providing information about the actual return value will likely result in more helpful information about what actually happened instead.

The current information only reveals that the intended effect (throw) did not occur, but gives no single insight into what actually happened instead.

This leaves the user uninformed of the actual result that was returned.

This in contrast to other assertions such as .toMatchObject() which reports the changes in high detail.

Example

First, with no expectations in .toThrow():

expect(() => ({ id: 1 })).toThrow()

Error: expect(received).toThrow()

Received function did not throw
Returned: { id: 1 }

Second, with a matcher:

expect(() => ({ id: 1 })).toThrow(expect.any(String))

Error: expect(received).toThrow(expected)

Expected asymmetric matcher: Any<String>

Received function did not throw
Returned: { id: 1 }

Pitch

This feature belongs in the jest platform because the existing testing experience for other matchers provides information about the received outcome when it differs from the expected result. The received outcome is currently reported, but with insufficient detail to make any meaningful deductions if the logic under test is non-trivial. In many cases the return value is highly likely to reveal at least in part what code-path was taken or perhaps what value thwarted the intended code-path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant