Skip to content

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

Open
@kirkwaiblinger

Description

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 #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, jest@29.7.0

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions