Docs (4.0.0)
class Assertion method

toHaveProperty

Asserts that the value is an object that includes a property with given name (using the in operator).

toHaveProperty(propertyName: string): Assertion<unknown>

Notes

  • This method also succeeds for properties that are defined on the object’s prototype.

Return value

A new assertion for the value of the property, which can be evaluated using further Assertion methods.

Example

// assert the value of a property, checking that it exists first:
let obj = { foo: "bar" };
expect(obj).toHaveProperty("foo").toBe("bar");

Related