Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deep.property not behaving #1007

Closed
jmls opened this issue Jul 16, 2017 · 2 comments
Closed

deep.property not behaving #1007

jmls opened this issue Jul 16, 2017 · 2 comments

Comments

@jmls
Copy link

jmls commented Jul 16, 2017

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 ?

@meeber
Copy link
Contributor

meeber commented Jul 16, 2017

@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});
        })

@jmls
Copy link
Author

jmls commented Jul 16, 2017

ok, cool. thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants