Open elliot-tt opened 2 months ago
The main problem here is that toHaveReceivedCommandWith()
does not simply compare 2 objects but compares the expected object with all received values, of which there can be 0, 1, or multiple. So if we received 2 calls and none of them match, we don't know to which we should compare in a "nice" output.
However:
toHaveReceivedNthCommandWith()
which compares expected value and single received value, so we can show the nice diff here as well.This sounds like a nice improvement. If you are willing to do so, please create a PR.
Love the library. It is much more ergonomic compared to the mocking I was doing by hand!
Jest can show a diff between the received and actual calls when I use
.toHaveBeenCalledWith(...)
including when there are multiple non-matches.Diff Examples in a Details so this ticket doesn't look super long
```ts describe('diff demo', () => { it('diffs the real call and expected', () => { const mockFn = jest.fn(); mockFn({ RoleArn: 'abc', RoleSessionName: 'def' }); expect(mockFn).toHaveBeenCalledWith({ SomeField: 'xyz', RoleArn: 'ABC', RoleSessionName: 'def', }); }); }); ``` ```diff expect(jest.fn()).toHaveBeenCalledWith(...expected) - Expected + Received Object { - "RoleArn": "ABC", + "RoleArn": "abc", "RoleSessionName": "def", - "SomeField": "xyz", }, Number of calls: 1 ``` ```ts it('diffs the real call and expected with multiple calls', () => { const mockFn = jest.fn(); mockFn({ RoleArn: 'abc', RoleSessionName: 'def' }); mockFn({ SomeField: 'xyz', RoleSessionName: 'def' }); expect(mockFn).toHaveBeenCalledWith({ SomeField: 'xyz', RoleArn: 'ABC', RoleSessionName: 'def', }); }); ``` ```diff expect(jest.fn()).toHaveBeenCalledWith(...expected) Expected: {"RoleArn": "ABC", "RoleSessionName": "def", "SomeField": "xyz"} Received 1 Object { - "RoleArn": "ABC", + "RoleArn": "abc", "RoleSessionName": "def", - "SomeField": "xyz", }, 2 Object { - "RoleArn": "ABC", "RoleSessionName": "def", "SomeField": "xyz", }, Number of calls: 2 ```Whereas when I use
aws-sdk-client-mock-jest
matcher.toHaveReceivedCommandWith
particularly when the difference between expected and actual is subtle (the example that prompted this feature request was an arn with a
:
v.s.::
it would be nice to see the diff output (which I naively assume Jest can do for us? 🤞)If this is a feature you'd support I'd happily contribute some time to making it happen.