-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow to test for Error instance type #1636
Comments
This should already be implemented. Please provide reproduction. |
It works for an error class, I'd say it's only a matter of documenting it here: https://vitest.dev/api/#tothrowerror You can provide an optional argument to test that a specific error is thrown:
- regular expression: error message matches the pattern
- string: error message includes the substring
+ - error class: error object is instance of class Passing an error instance does not work though, reproduction here: https://stackblitz.com/edit/vitest-dev-vitest-cdj66q?file=test/basic.test.ts I'd expect vitest to compare class instance, and the message to match in this case. It's stated as such in Jest's documentation: |
@MonsieurMan your reproduction works if I use - expect(() => throwError()).throws(CustomErrorClass);
+ expect(() => throwError()).toThrowError(CustomErrorClass); |
Will do tomorrow, maybe it's just a matter of documenting it then ! :)
…On Wed, 13 Jul 2022, 22:37 Vladimir, ***@***.***> wrote:
This should already be implemented. Please provide reproduction.
—
Reply to this email directly, view it on GitHub
<#1636 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADEAHRU7WFY6GS24PM7A2WDVT4SKFANCNFSM53PMMAPQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Clear and concise description of the problem
I like to use custom Error types to help my user manage errors effectively, and I'd like a way to test it simply in vitest.
It seems Jest allows to do so using: https://jestjs.io/docs/expect#tothrowerror, which states it can accept an
error class: error object is instance of class
.I tried to find a previous issue or discussion about it in vitest but couldn't, so here's one :)
Suggested solution
Jest implementations allow to do so :
Alternative
As of now, I wrote a test I don't really like which uses a
try/catch
and an expect(true).to.eq(false) at the end to make it fail if there was no error.Additional context
I guess it's here I tell you that Vitest is such a pleasure to use and you guys do a great job :)
Happy to help or provide further information if needed !
Validations
The text was updated successfully, but these errors were encountered: