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]: assert.throws errors always formatted as not thrown #13264

Closed
theScottyJam opened this issue Sep 14, 2022 · 4 comments · Fixed by #13322
Closed

[Bug]: assert.throws errors always formatted as not thrown #13264

theScottyJam opened this issue Sep 14, 2022 · 4 comments · Fixed by #13322

Comments

@theScottyJam
Copy link

Version

29.0.3

Steps to reproduce

git clone https://github.com/theScottyJam/jest-assert-throws-bug-report
cd jest-assert-throws-bug-report/
npm i
npm test

Expected behavior

When Node's assert.throws() assertion error is caught and reported by Jest, it should report the original error message, or at least report an accurate error message.

Actual behavior

Jest will always report an assert.throws() error with the following message:

Expected the function to throw an error.
But it didn't throw anything.

Additional context

The reproducible example contains two test cases. The first will cause assert.throws() to fail because the message property contains the incorrect content. The second will cause assert.throws() to fail because the wrong error class was thrown. In both cases, you'll find that Jest will report the error as "Expected the function to throw an error. But it didn't throw anything". To proove that Jest is incorrectly reporting the error, I've also caught and logged out these assertion errors. You can find the logged errors above the Jest output. There, you'll find accurate output such as:

    The actual error: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
    + actual - expected
    
      Comparison {
    +   message: 'message 1'
    -   message: 'message 2'
      }

and

    The actual error: AssertionError [ERR_ASSERTION]: The "TypeError" validation function is expected to return "true". Received TypeError: SyntaxError: message 1

Environment

System:
    OS: Linux 5.19 Fedora Linux 36 (Workstation Edition)
    CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
  Binaries:
    Node: 16.14.0 - /usr/bin/node
    npm: 8.3.1 - /usr/bin/npm
  npmPackages:
    jest: ^29.0.3 => 29.0.3
@SimenB
Copy link
Member

SimenB commented Sep 15, 2022

Ah, good catch! That's a bug in how we format assert.throws errors - we just assume the issue when it fails is no throw instead of inspecting.

PR very much welcome fixing!

The code is here https://github.com/facebook/jest/blob/e703e6ed1861595d56e25a764642da5a32b32c5a/packages/jest-circus/src/formatNodeAssertErrors.ts#L152-L160 and here https://github.com/facebook/jest/blob/e703e6ed1861595d56e25a764642da5a32b32c5a/packages/jest-jasmine2/src/assertionErrorMessage.ts#L113-L121

(Might be a good idea as part of this to share that code somehow, but definitely not needed!)

And the current test is here https://github.com/facebook/jest/blob/e703e6ed1861595d56e25a764642da5a32b32c5a/e2e/failures/__tests__/assertionError.test.js#L75-L77

@shraddhafalane
Copy link

I would like to work on this issue

@SimenB
Copy link
Member

SimenB commented Sep 16, 2022

Awesome, go for it!

@SimenB SimenB changed the title [Bug]: [Bug]: assert.throws errors always formatted as not thrown Sep 16, 2022
lpizzinidev added a commit to lpizzinidev/jest that referenced this issue Sep 26, 2022
lpizzinidev added a commit to lpizzinidev/jest that referenced this issue Sep 27, 2022
@github-actions
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 Oct 30, 2022
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.

3 participants