question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toHaveBeenCalledOnceWith only asserts a match on the first parameter.

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
loyajcommented, 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.

0reactions
keeganwittcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

New matchers: toHaveBeenCalledOnce and ... - GitHub
Ive got a branch locally with a toHaveBeenCalledOnce matcher, it was queit straightforward, however the toHaveBeenCalledOnceWith is causing my ...
Read more >
jasmine toHaveBeenCalledWith partial matching
With Jasmine, I could spy on methods and figure out the arguments. I want to be able to call toHaveBeenCalledWith(something, anything) .
Read more >
Jest assert over single or specific argument/parameters with ...
With Jest it's possible to assert of single or specific arguments/parameters of a mock function call with .toHaveBeenCalled /
Read more >
toHaveBeenCalledWith / checking a specific parameter
I've been looking at Jasmine for the first time, I am impressed. It ... The only way I can see to do this...
Read more >
Expect - Jest
Matchers should return an object (or a Promise of an object) with two keys. pass indicates whether there was a match or not,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found