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

Allow to test for Error instance type #1636

Closed
4 tasks done
MonsieurMan opened this issue Jul 13, 2022 · 4 comments
Closed
4 tasks done

Allow to test for Error instance type #1636

MonsieurMan opened this issue Jul 13, 2022 · 4 comments

Comments

@MonsieurMan
Copy link

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 :

test('Custom error instance', () => {
  expect(drinkOctopus).toThrowError(DisgustingFlavorError);
});

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

@sheremet-va
Copy link
Member

This should already be implemented. Please provide reproduction.

@MonsieurMan
Copy link
Author

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: error object: error message is equal to the message property of the object

@sheremet-va
Copy link
Member

sheremet-va commented Jul 19, 2022

@MonsieurMan your reproduction works if I use toThrowError instead of throws which is chai function, and is not documented as Jest-compatible:

- expect(() => throwError()).throws(CustomErrorClass);
+ expect(() => throwError()).toThrowError(CustomErrorClass);

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Jul 31, 2022
@MonsieurMan
Copy link
Author

MonsieurMan commented Oct 11, 2022 via email

@github-actions github-actions bot locked and limited conversation to collaborators Jun 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants