-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
How are you suppose to use .toThrow() ? #781
Comments
Thank you for creating an issue but please ask questions on stackoverflow or discord: facebook.github.io/jest/support.html An example of expect(() => {
throw new Error();
}).toThrow(); you should wrap your |
@kopax apparently you need to do
Not entirely obvious from the example given, but that resolved my issue |
If you what you are testing is short, you can omit the
|
- wrapped function in call function so toThrow() works properly. see jestjs/jest#781 & http://jasmine.github.io/2.0/introduction.html
toThrow executes a function passed in expect, and verifies that it throws an error. In this case, you're not passing expect a function. You're executing a function that throws an error. toThrow is never executed, and even if it were, it would throw an error, telling you that it should receive a function, but received something else instead.
Sorry for the necrobumps 😅. Just wanted to let future people know why this happens. |
|
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. |
I didn't know if there was a community forum or an active stack tag. So I thought I would post this here.
If we look at this function also shown below:
We can see that if obj is of type string that we can throw an error. So I though I could write a jest test as such:
How ever when run:
So I am confused. How do I test that an error was thrown with out actually throwing the error? If that makes sense? Like is there an issue with how I throw the error or with jest or my test ...
The text was updated successfully, but these errors were encountered: