Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve toHaveBeenCalledExactlyOnceWith messages #675

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

forivall
Copy link

@forivall forivall commented Jan 26, 2024

What

Improve the output of toHaveBeenCalledExactlyOnceWith to display a diff when the arguments do not match.

Notably, I use printDiffOrStringify to improve readability with many arguments or with complex arguments. Jest's core expect uses a diff matcher as well, although it has added complexity to display the diff without indentation. I think the solution I've implemented here is a happy middle ground.

In the following example, it's hard to tell what the incorrect call was, because the second argument is not printed, and, if it was complex, it would be hard to read.

Before:

expect(received).toHaveBeenCalledExactlyOnceWith(expected)

Expected mock function to have been called exactly once with ["hello", "where"], but it was called with "hello""

After:

expect(received).toHaveBeenCalledExactlyOnceWith(expected)

- Expected arguments  - 1
+ Received arguments  + 1

  Array [
    "hello",
-   "where",
+   "there",
  ]

Why

Fixes #645, fixes #672, and fixes #690, and the output more closely resembles the output of an argument mismatch when using jest's toHaveBeenCalledWith.

Notes

Housekeeping

  • Unit tests
  • Documentation is up to date
  • No additional lint warnings
  • Typescript definitions are added/updated where relevant

Copy link

changeset-bot bot commented Jan 26, 2024

🦋 Changeset detected

Latest commit: 6efcb87

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
jest-extended Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@aarowman
Copy link

Hi, can we get this merged? I've been having issues with the messages for nearly a year now, and would love to see this added!

@tgfisher4
Copy link

Just ran into this puzzling error reporting myself... @mattphillips, sorry for the direct ping, but might you have some time to take a look here? 🥺 🙇 This PR will address at least 3 issues (#645 #672 #690)

# for free to join this conversation on GitHub. Already have an account? # to comment