how to pass expect.toMatchObject() with Date as one of the expected property value using jest?

Viewed 605

I have an express API endpoint which returns an object and on that object there is a property(property3 in our example below) which has a date as it's value.

For e.g

  res.body = {
       "property1": "value1"
       "property2": "value2"
       "property3": new Date()
    }

I am performing test on this endpoint using jest and supertest.

Since date.now() !== date.now(), how will i pass

expect(res.body).toMatchObject(<expectedObject>)

1 Answers

You can use expect.objectContaining which matches subset properties only. Above example can write as following:

expect(res.body).toMatchObject(expect.objectContaining({
  "property1": "value1"
  "property2": "value2"
}))
Related