Skip to content

[Bug]: toThrow does not check error cause #15111

@DoroGi

Description

@DoroGi

Version

29.7.0

Steps to reproduce

it('should check error cause', () => {
  const fn = () => {
    throw new Error('error message', { cause: { code: '1234' } });
  };
  expect(fn).toThrow({
    name: 'Error',
    message: 'error message',
    cause: { code: 'random string' },
  });
});

Expected behavior

I expect the test above to fail, as the two causes are different.
The error message is being checked, but not the cause.

Actual behavior

The test pass independently from the cause field.

Additional context

No response

Environment

System:
  OS: macOS 14.4.1
  CPU: (10) arm64 Apple M1 Pro
Binaries:
  Node: 20.12.2 - /usr/local/bin/node
  npm: 10.5.0 - /usr/local/bin/npm
npmPackages:
  jest: 29.7.0 => 29.7.0

Metadata

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