expect.objectContaining(object)

expect.objectContaining(object) matches any received object that recursively matches the expected properties. That is, the expected object is a subset of the received object. Therefore, it matches a received object which contains properties that are present in the expected object.

Instead of literal property values in the expected object, you can use matchers, expect.anything(), and so on.

For example, let's say that we expect an onPress function to be called with an Event object, and all we need to verify is that the event has event.x and event.y properties. We can do that with:

  1. test('onPress gets called with the right thing', () => {
  2. const onPress = jest.fn();
  3. simulatePresses(onPress);
  4. expect(onPress).toBeCalledWith(
  5. expect.objectContaining({
  6. x: expect.any(Number),
  7. y: expect.any(Number),
  8. }),
  9. );
  10. });