SmellydogCoding
SmellydogCoding

Reputation: 464

Testing for an object key in an array of objects using mocha and chai

If I have an object that is an array of objects

{ "places": [ {city: "Pittsburgh", state: "PA"} ] }

how do I test to see if the array at index 0 (places[0]) has an object with a key "city"? I tried this but it didn't work

expect( {"places": [ {"city": "Pittsburgh", "state": "PA"} ] } ).to.nested.include({"places[0]" : "city"});

Upvotes: 3

Views: 1844

Answers (1)

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10356

You should use to.have.nested.property instead of to.nested.include:

var obj = {"places": [ {"city": "Pittsburgh", "state": "PA"} ] };

expect(obj).to.have.nested.property("places[0].city");

Upvotes: 6

Related Questions