.toHaveProperty()

This assertion checks for a the presence of a property. The value of the proprty can also optionally be chcked.

Presence

jestUnexpected({
    foo: 1,
    baz: {
        baz: null
    }
}).toHaveProperty('foo.bar');
expected { foo1baz: { baznull } } to have property
KeyPathSpec({
  
spec'foo.bar'valueundefinednestedfalse,
isValueExplicitfalse
})
 
{
  
foo
1
//
//
 
⨯ 
should be an object
 and
⨯ 
should have property PropOrUndefined({ value'bar' })
  
baz: { baznull }
}

Value

You can also specify the value the property must have.

jestUnexpected({
    foo: {
        bar: "baz"
    },
    baz: {
        baz: null
    }
}).toHaveProperty('foo.bar', 'bar')
expected { foo: { bar'baz' }, baz: { baznull } }
to have property KeyPathSpec({ spec'foo.bar'value'bar'nestedfalseisValueExplicittrue })
 
{
  
foo: {
  
bar
'baz' 
//
//
//
//
 
should equal 
'bar'
 
baz
bar
},
  
baz: { baznull }
}