Description
🚀 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.