Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: toThrow does not fail test on mismatch of Error#cause #15316

Closed
steveluscher opened this issue Sep 20, 2024 · 5 comments · May be fixed by #15339
Closed

[Bug]: toThrow does not fail test on mismatch of Error#cause #15316

steveluscher opened this issue Sep 20, 2024 · 5 comments · May be fixed by #15339

Comments

@steveluscher
Copy link

steveluscher commented Sep 20, 2024

Version

30.0.0-alpha.6

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

The test should fail, owing to the mismatch of Error#cause.

Actual behavior

The test passes.

Additional context

This is a re-file of #15111 which was auto-closed for inactivity. Updated to note that this is still a problem in Jest v30.

This appears to be because Jest only does cause-matching when the cause property is an instance of Error.

https://github.com/jestjs/jest/blob/main/packages/expect/src/toThrowMatchers.ts#L481-L483

This is inappropriate, since the value of cause is allowed to be any type.

Environment

System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (64) x64 AMD EPYC 7502P 32-Core Processor
  Binaries:
    Node: 20.16.0 - ~/n/bin/node
    Yarn: 1.22.22 - ~/.local/share/pnpm/yarn
    npm: 10.8.1 - ~/n/bin/npm
    pnpm: 9.9.0 - ~/.local/share/pnpm/pnpm
  npmPackages:
    jest: ^30.0.0-alpha.6 => 30.0.0-alpha.6
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 8, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 8, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 8, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 8, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 8, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 9, 2024
BondarenkoAlex added a commit to BondarenkoAlex/jest that referenced this issue Oct 17, 2024
Copy link

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 github-actions bot added the Stale label Oct 20, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 19, 2024
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@steveluscher
Copy link
Author

Folks, there's a PR and everything, and it even protects against circular references. Let's land it!

Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Dec 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant