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

toHaveBeenCalledOnceWith only asserts a match on the first parameter. #517

Closed
GRollason opened this issue Oct 12, 2022 · 7 comments
Closed

Comments

@GRollason
Copy link

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
Copy link

loyaj commented Oct 22, 2022

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?

@keeganwitt
Copy link
Collaborator

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

@keeganwitt
Copy link
Collaborator

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

@loyaj
Copy link

loyaj commented Oct 25, 2022

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.

@keeganwitt
Copy link
Collaborator

keeganwitt commented Oct 25, 2022

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

@keeganwitt
Copy link
Collaborator

keeganwitt commented Nov 8, 2022

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.

@Maxim-Mazurok
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants