jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: toThrow fails if error object contains a cause property

Open kirkwaiblinger opened this issue 1 year ago • 5 comments

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]

kirkwaiblinger avatar Jul 14 '24 21:07 kirkwaiblinger

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.

github-actions[bot] avatar Aug 13 '24 22:08 github-actions[bot]

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 avatar Aug 23 '24 18:08 BondarenkoAlex

@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.

kirkwaiblinger avatar Aug 23 '24 23:08 kirkwaiblinger

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.

github-actions[bot] avatar Sep 23 '24 00:09 github-actions[bot]

Not stale, just waiting to be addressed

kirkwaiblinger avatar Sep 23 '24 01:09 kirkwaiblinger

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.

github-actions[bot] avatar Oct 23 '24 02:10 github-actions[bot]

Bump

kirkwaiblinger avatar Oct 23 '24 04:10 kirkwaiblinger

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.

github-actions[bot] avatar Nov 22 '24 05:11 github-actions[bot]

Bump

kirkwaiblinger avatar Nov 22 '24 08:11 kirkwaiblinger

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.

github-actions[bot] avatar Dec 22 '24 10:12 github-actions[bot]

Bump

kirkwaiblinger avatar Dec 22 '24 14:12 kirkwaiblinger

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.

github-actions[bot] avatar Jan 23 '25 09:01 github-actions[bot]

Bump

kirkwaiblinger avatar Jan 23 '25 16:01 kirkwaiblinger

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.

github-actions[bot] avatar Feb 22 '25 18:02 github-actions[bot]

Bump

kirkwaiblinger avatar Feb 22 '25 21:02 kirkwaiblinger

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.

github-actions[bot] avatar Mar 24 '25 22:03 github-actions[bot]

Bump

kirkwaiblinger avatar Mar 24 '25 22:03 kirkwaiblinger

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.

github-actions[bot] avatar Apr 23 '25 23:04 github-actions[bot]

Bump

kirkwaiblinger avatar Apr 23 '25 23:04 kirkwaiblinger

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.

github-actions[bot] avatar May 24 '25 00:05 github-actions[bot]

Bump

kirkwaiblinger avatar May 24 '25 00:05 kirkwaiblinger

Fixed in Jest 30, beta 4 is out now.

cpojer avatar May 28 '25 06:05 cpojer

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.

github-actions[bot] avatar Jun 28 '25 00:06 github-actions[bot]