79316316

Date: 2024-12-29 20:50:25
Score: 1
Natty:
Report link

No, this is not a bug. In toHaveBeenCalledWith, the arguments are checked positionally, as explained in the first comment.

If you run into this problem and would like to avoid it for console.log specifically, a relatively simple fix is to use template strings in Javascript instead of passing arguments that console.log concatenates with a space. A template string uses backticks (`) to delineate the string, and will execute code which is placed between ${ and } inside the string. The result is a single string, parts of which are the result of executing the code in the ${} parts, and so console.log will get called with a single argument. Thus, your call to any of the various Jest matchers will see the whole string as the first argument.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Dave Scotese