Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>)

like image 570
kob003 Avatar asked Oct 19 '25 11:10

kob003


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"
}))
like image 98
tmhao2005 Avatar answered Oct 21 '25 12:10

tmhao2005



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!