I'm trying to test a function that returns a promise using the chai-as-promised library. The result in my promise is an object with nested properties. Is it possible to test a deeply nested property's value.
E.g.
function myFunc() {
return new Promise((resolve, reject) => {
const data = {
thing: {
foo: 'bar',
baz: 'lah'
}
}
resolve(data)
})
}
How can I test that the foo
property equals "bar" without checking the entire object? I've tried something like this:
expect(myFunc()).to.eventually.have.property('thing.foo', 'bar')
But no luck!
Using deep property lookup should work. Just add the deep
keyword before property
.
expect(myFunc()).to.eventually.have.deep.property('thing.foo', 'bar')
If you prefer the verbose way, you should also be able to do stuff like:
expect(myFunc())
.to.eventually.have.property('thing')
.that.has.property('foo')
.that.is.equal.to('bar');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With