You can use a combination of arrayContaining
and objectContaining
to make this work.
Reference:
- https://jestjs.io/docs/expect#expectarraycontainingarray
- https://jestjs.io/docs/expect#expectobjectcontainingobject
Here is some sample code for you:
function something(a, b, somefn) {
somefn([{
x: a,
y: b,
id: 'some-guid'
}]);
}
test('Testing something', () => {
const mockSomeFn = jest.fn();
something(2, 3, mockSomeFn);
expect(mockSomeFn).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
x: 2,
y: 3
})
])
);
});
Sample output:
$ jest
PASS ./something.test.js
✓ Testing something (3 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.257 s, estimated 1 s
Ran all test suites.
✨ Done in 0.84s.
Here is some explanation:
toHaveBeenCalledWith
is called withexpect.arrayContaining
which verifies if it was called with an arrayexpect.arrayContaining
has an array. The array has an object withobjectContaining
which does the partial match against the object.