admin管理员组

文章数量:1287247

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!

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!

Share Improve this question edited Jun 6, 2016 at 9:46 harryg asked Jun 6, 2016 at 9:38 harrygharryg 24.1k47 gold badges134 silver badges208 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

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

本文标签: javascriptTesting value of nested property in chaiaspromised and mochaStack Overflow