jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.22k stars 6.46k forks source link

Improve messaging for failed toHaveBeenCalledWith assertions #3523

Closed quantizor closed 7 years ago

quantizor commented 7 years ago

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 commented 7 years ago

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

quantizor commented 7 years ago

I don't have time right now, just flagging it in case someone has interest. On Wed, May 10, 2017 at 1:38 PM Michał Pierzchała notifications@github.com wrote:

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/facebook/jest/issues/3523#issuecomment-300558324, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiy1mpc2lSNZwzxY6DW7Lz3_F97SIcFks5r4fYlgaJpZM4NUhct .

rubenmoya commented 7 years ago

I'd love to give it a try :)

rubenmoya commented 7 years ago

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 commented 7 years ago

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

mkubilayk commented 7 years ago

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 commented 7 years ago

@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[bot] commented 3 years ago

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.