jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

Improve toHaveBeenCalledExactlyOnceWith messages

Open forivall opened this issue 5 months ago • 1 comments

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

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

forivall avatar Jan 26 '24 00:01 forivall