Chai: deep.property not behaving

Created on 16 Jul 2017  Â·  2Comments  Â·  Source: chaijs/chai

I have these 2 tests

describe('the create() method ', (() => {
        it('test#1 should fail if there is no code supplied', () => {
            return foo.create({}) .should.be.rejected.and.eventually.to.have
              .nested.property('details.codes.code[0]').that.eql('presence');
        })

        it('test #2 should fail if there is no code supplied', () => {
            return foo.create({}).should.be.rejected.and.eventually.to.have
              .deep.property('details.codes.code[0]','presence');
        })


    }))

When I run the test, I get the following result

   the create() method
      ✓ test#1 should fail if there is no code supplied
      1) test #2 should fail if there is no code supplied


  1 passing (4s)
  1 failing

  1)  when testing the foo model  the create() method test #2 should fail if there is no code supplied:
     AssertionError: expected { Object (name, message, ...) } to have deep property 'details.codes.code[0]'

is there something wrong with the way I am using deep.property ?

Most helpful comment

@jmls Starting with Chai v4, .nested enables the "nested syntax" (e.g., details.code.code[0]), and .deep enables deep equality comparison to be used as opposed to strict (===) comparison.

So for your second example... if the value of the property that you're checking is just a string, strict equality should be fine, so you don't need the .deep flag, you only need the .nested flag:

        it('test #2 should fail if there is no code supplied', () => {
            return foo.create({}).should.be.rejected.and.eventually.to.have
              .nested.property('details.codes.code[0]','presence');
        })

But if you were instead comparing the value against an object (e.g., {a: 1}) and wanted to use deep equality, then you would combine the .nested and .deep flags:

        it('test #2 should fail if there is no code supplied', () => {
            return foo.create({}).should.be.rejected.and.eventually.to.have
              .deep.nested.property('details.codes.code[0]', {a: 1});
        })

All 2 comments

@jmls Starting with Chai v4, .nested enables the "nested syntax" (e.g., details.code.code[0]), and .deep enables deep equality comparison to be used as opposed to strict (===) comparison.

So for your second example... if the value of the property that you're checking is just a string, strict equality should be fine, so you don't need the .deep flag, you only need the .nested flag:

        it('test #2 should fail if there is no code supplied', () => {
            return foo.create({}).should.be.rejected.and.eventually.to.have
              .nested.property('details.codes.code[0]','presence');
        })

But if you were instead comparing the value against an object (e.g., {a: 1}) and wanted to use deep equality, then you would combine the .nested and .deep flags:

        it('test #2 should fail if there is no code supplied', () => {
            return foo.create({}).should.be.rejected.and.eventually.to.have
              .deep.nested.property('details.codes.code[0]', {a: 1});
        })

ok, cool. thanks for the explanation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

domenic picture domenic  Â·  4Comments

meeber picture meeber  Â·  3Comments

danthegoodman picture danthegoodman  Â·  3Comments

endymion00 picture endymion00  Â·  3Comments

ghost picture ghost  Â·  4Comments