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

Improve messaging for failed toHaveBeenCalledWith assertions #3523

Closed
quantizor opened this issue May 8, 2017 · 8 comments · Fixed by #3913
Closed

Improve messaging for failed toHaveBeenCalledWith assertions #3523

quantizor opened this issue May 8, 2017 · 8 comments · Fixed by #3913

Comments

@quantizor
Copy link
Contributor

quantizor commented May 8, 2017

Currently if you are using expect(fn).toHaveBeenCalledWith(expect2, expect2) perhaps like:

const fn = jest.fn();

fn(undefined, {foo: 'bar', fizz: 'buzz'});

expect(fn).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
  fizz: 'buzz',
}));

Error output:

expect(jest.fn()).toHaveBeenCalledWith(expected)
    
    Expected mock function to have been called with:
      [Anything, ObjectContaining {"fizz": "buzz"}]
    But it was called with:
      [undefined, {"fizz": "buzz", "foo": "bar"}]

The resulting error message doesn't specify which argument lead to the failed assertion. It simply pretty-prints the mock call arguments and says something failed. This can be difficult to debug if those arguments are particularly large objects, etc.

It would be helpful to point out which argument(s) contributed to the assertion fail, like:

"Argument 1 did not satisfy the expectation expect.anything(), received undefined." or
"Argument 2 did not satisfy the expectation expect.objectContaining({fizz: 'buzz'}), received <CODE>."

@thymikee
Copy link
Collaborator

Would you be interested in sending PR with the enhancement? :)

@thymikee thymikee changed the title [Feature Request] Better messaging for failed toHaveBeenCalledWith assertions Improve messaging for failed toHaveBeenCalledWith assertions May 10, 2017
@quantizor
Copy link
Contributor Author

quantizor commented May 10, 2017 via email

@rubenmoya
Copy link
Contributor

I'd love to give it a try :)

@rubenmoya
Copy link
Contributor

rubenmoya commented May 12, 2017

Hi! So I've been looking at the code for createToBeCalledWithMatcher and trying to print the errors with the new format, but I found that when we compare the arguments with the expected ones, I only know that they're different, but not which one is the different one.

Here instead of .some I've used a .filter to loop through the calls later, but I need that calls to have the failing arguments filtered to...

Any ideas? How could I approach this?

Thank you!

UPDATE: Nevermind, got it, but the code is pretty ugly, I'll spend more tame later today improving it :)

screen shot 2017-05-12 at 13 36 06

@quantizor
Copy link
Contributor Author

There's a lodash method called "partition" that might be good for this @rubenmoya

@mkubilayk
Copy link
Contributor

I have a working version for this feature but before sending the PR, I wanted to clarify error messages. Should we only include the first failed argument from a single call? Or all failed argument assertions from all calls? Any ideas @thymikee?

@thymikee
Copy link
Collaborator

@mkubilayk let's ship the PR and we'll discuss on the real thing :). But I'd rather like to see all failed arguments from relevant calls.

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

Successfully merging a pull request may close this issue.

5 participants
@quantizor @rubenmoya @mkubilayk @thymikee and others