jest-community/jest-extended

toHaveBeenCalledOnceWith only asserts a match on the first parameter.

GRollason opened this issue · 7 comments

I'm not sure if this would be classed as a bug or a feature, so apologies for any mistakes raising the issue.

Feature Request

Description: toHaveBeenCalledOnceWith currently only matches against the first parameter provided to a mock function.

Possible solution:
In toHaveBeenCalledOnceWith.js, line 40 reads
const pass = passOnce && this.equals(expected, received.mock.calls[0][0]);
I believe that this should probably be changed to this.equals(expected, received.mock.calls[0]); and assert equality on the whole array of parameters passed to the mock function in the first call. This may need the function declaration to be changed to toHaveBeenCalledOnceWith(received, ...expected) or similar to handle multiple arguments.

If this is desired, or not so simple to implement, it would be good to make this clear in the documentation.

loyaj commented

I'm seeing the same behavior, and it's really unexpected to me. The toHaveBeenCalledOnceWith matcher is named to suggest it's a combination of toHaveBeenCalledTimes(1) and toHaveBeenCalledWith. What's the intended behavior?

@loyaj What you are suggesting is implied by the name is the intended behavior.

I'm going to close this one in favor of #518, just because that one more precisely describes the issue.

loyaj commented

Oh, I’m still thinking this issue is describing a separate problem from #518, is it not? Jest’s toHaveBeenCalledOnceWith matches on all arguments, not just the first one.

Ah. You're right. I was looking at was ultimately implemented, but that doesn't match what was described in #138.

I haven't found a way to make this work with varargs. Jest seems to pass only the first parameter as expected into the matcher. I'm looking in Jest code to see if there's a way this behavior can be changed, but I'm suspecting not.

That's a pretty big issue... We were about to introduce jest-extended to our project just because of this matcher, good thing that types didn't work :)
Would be great to see this fixed or removed if it can't be fixed, I imagine lots of people have flawed tests because of this misunderstanding.