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

.toThrow() to match both error constructor and message? #3659

Closed
OpenGG opened this issue May 26, 2017 · 5 comments
Closed

.toThrow() to match both error constructor and message? #3659

OpenGG opened this issue May 26, 2017 · 5 comments

Comments

@OpenGG
Copy link

OpenGG commented May 26, 2017

I want to request a feature.

What is the current behavior?

Currently .toThrow() can match error constructor or message, but not both: demo

expect(()=>{
  throw new Error('1234');
}).toThrow(Error, 'hello'); // passes, but not what users may expect

Instead, I have to do it like this: demo

const matcher = expect(()=>{
  throw new Error('1234');
});
matcher.toThrow(Error);
matcher.toThrow('1234');

What is the expected behavior?

Is it possible .toThrow() can be refactored to match both error constructor and message at the same time, if message is provided?

expect(()=>{
  throw new Error('1234');
}).toThrow(Error);  // error constructor

expect(()=>{
  throw new Error('1234');
}).toThrow('1234'); // string to match message

expect(()=>{
  throw new Error('1234');
}).toThrow(/1234/); // regex to match message

expect(()=>{
  throw new Error('1234');
}).toThrow(Error, '1234');  // error constructor with string to match message

expect(()=>{
  throw new Error('1234');
}).toThrow(Error, /1234/);  // error constructor with regex to match message
@cpojer
Copy link
Member

cpojer commented May 26, 2017

Please feel free to implement your own matcher with this feature using expect.extend.

@cpojer cpojer closed this as completed May 26, 2017
@Nevon
Copy link

Nevon commented Aug 11, 2018

Is this still the official position? I find myself wanting to do this, but without #6243 it's not really possible to overload the behavior of toThrow, so it would have to be a completely separate matcher.

@SimenB
Copy link
Member

SimenB commented Aug 13, 2018

Yeah, I think so. You can probably get it added to jest-extended, if you want.

@mattphillips

@mattphillips
Copy link
Contributor

@Nevon I've just opened up this issue in jest-extended to add this matcher if you want to help out by sending a PR 👍

@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 May 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants