[Bug]: toThrow fails if error object contains a cause property
Version
29.7.0
Steps to reproduce
expect(
(async () => {
throw new Error("message", { cause: new Error("cause") });
})(),
).rejects.toThrow(new Error("message"));
Expected behavior
Test should pass
Actual behavior
expect(received).rejects.toThrow(expected)
- Expected message - 1
+ Received message and cause + 8
Additional context
I am refiling this as a bug, because it was ignored and autoclosed as a documentation request, see https://github.com/jestjs/jest/issues/15072 🙁.
Please read through that issue first, as it also contains additional examples. IMO this may or may not be a bug, but, without documentation that speaks to this, I cannot tell, and it was unexpected to me.
Environment
macos, node 18, [email protected]
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
I am sorry for my English.
If you want to pass your test. You need compare with string or regexp or pass equal Error object
expect(
(async () => {
throw new Error("message", { cause: new Error("cause") });
})(),
).rejects.toThrow("message");
or
expect(
(async () => {
throw new Error("message", { cause: new Error("cause") });
})(),
).rejects.toThrow(/message/);
or
expect(
(async () => {
throw new Error("message", { cause: new Error("cause") });
})(),
).rejects.toThrow(new Error("message", { cause: new Error("cause") }));
the other case
expect(
(async () => {
throw new Error("message");
})(),
).rejects.toThrow(new Error("message"));
@BondarenkoAlex I hear you, and that's a perfectly reasonable behavior, but the docs explicitly say otherwise.
I no longer need a resolution to this, but I was reporting this in the hopes that either the docs or the behavior could get changed (whichever is intended) in order to be consistent, so that others don't get confused by the same hangup.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Not stale, just waiting to be addressed
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bump
Fixed in Jest 30, beta 4 is out now.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.