Even when the previous methods does work, it feels uncomfortable to access 0 indexes and nested properties and, in the end, doing manual comparisons. So I prefer this method:
expect(api.method).toBeCalledWith(
expect.not.objectContaining({
notWantedProperty: expect.anything(),
})
);
Also, this will check for previous calls and not only the indexed call. And looks better on the logs:
Expected: ObjectNotContaining {"name": Anything}
Received: ...
Be careful, since it will try to match the whole object and not every property separately. For example, if your api has been called with the following object:
{
"foo": "foo",
"bar": "bar"
}
The following expect would NOT fail:
expect(api.method).toBeCalledWith(
expect.not.objectContaining({
foo: expect.anything(),
baz: expect.anything(),
})
);
Because you didn't call the API with both properties, so I would create a new expect for every property:
expect(api.method).toBeCalledWith(
expect.not.objectContaining({ foo: expect.anything() })
);
expect(api.method).toBeCalledWith(
expect.not.objectContaining({ baz: expect.anything() })
);