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

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 and fixes #672, 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

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