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

make throwingMatcher test n arguments (currently zero or one) #7000

Closed
ericprud opened this issue Sep 19, 2018 · 5 comments
Closed

make throwingMatcher test n arguments (currently zero or one) #7000

ericprud opened this issue Sep 19, 2018 · 5 comments

Comments

@ericprud
Copy link

🚀 Feature Proposal

Make throwingMatcher test all arguments.

Motivation

Chai's throw takes multiple arguments to test error type and message.
Jest users who wish to test both of these traits must catch the error and manually perform both tests.
It would be trivial to make throwingMatcher's processResults apply each arg in turn:

      args.forEach(arg =>
        potentialResult = matcher.apply(matcherContext, [actual].concat([arg]));

Example

describe('throwingMatcher', () => {
  test('could test all args', () => {
    expect(() => {
      throw TypeError('ABC')
    }).toThrowError(TypeError, 'XYZ')
  })
})

would fail with:

    Expected the function to throw an error matching:
      "XYZ"
    Instead, it threw:
      TypeError: ABC

Pitch

  • known to be useful in chai.
  • trivial to implement
  • encourages more comprehensive testing
@SimenB
Copy link
Member

SimenB commented Sep 19, 2018

You can create a custom matcher for this, or get it into jest-extended, cc @mattphillips

Not sure we need it in core?

@natealcedo
Copy link
Contributor

@SimenB, this was actually proposed there already. I've started implementing some of it but I've been terribly busy at work so I haven't had the time to submit a PR there. Refer to jest-community/jest-extended#157. But if @ericprud is comfortable with working on it that would be cool as well.

@mattphillips
Copy link
Contributor

Yup this should be available in jest-extended soon so not sure we need it in core when we currently have toThrow in core.

@SimenB
Copy link
Member

SimenB commented Sep 21, 2018

Ah perfect.

This is apparently a duplicate of #3659 as well, so closing 🙂

@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 12, 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

4 participants