1

I have the following Chai + Mocha code, of which the results are very confusing me:

describe('Have Object Property', () => {
  it('should have the object property', () => {
    expect({x: {a: 1}}).to.have.property('x', {a: 1})
  })
  it('should have the property of an object property', () => {
    expect({x: {a: 1}}).to.have.property('x.a', 1)
  })
  it('should deeply have the object property', () => {
    expect({x: {a: 1}}).to.have.deep.property('x', {a: 1})
  })
  it('should deeply have the property of an object property', () => {
    expect({x: {a: 1}}).to.have.deep.property('x.a', 1)
  })
})

The results are:

Have Object Property
  1) should have the object property
  2) should have the property of an object property
  3) should deeply have the object property
  ✓ should deeply have the property of an object property


2 passing (53ms)
3 failing

  1) Have Object Property should have the object property:
     AssertionError: expected { x: { a: 1 } } to have a property 'x' of { a: 1 }, but got { a: 1 }
      at Context.<anonymous> (test/test.js:7:33)

  2) Have Object Property should have the property of an object property:
     AssertionError: expected { x: { a: 1 } } to have a property 'x.a'
      at Context.<anonymous> (test/test.js:10:33)

  3) Have Object Property should deeply have the object property:
     AssertionError: expected { x: { a: 1 } } to have a deep property 'x' of { a: 1 }, but got { a: 1 }
      at Context.<anonymous> (test/test.js:13:38)

In the four cases:

  1. why I cannot directly put {a: 1} as the value of property x?
  2. the object has no obj["x.a"] property
  3. using deep with only a single property (no dot) is the same as not using deep
  4. only this is working, but this is really not as convenient and directly as the first case.

Update and Comments on the duplicate mark:

What I am expecting, according to Chai's deep doc, is that, case 3 should pass, which is not.

I knew there is a similar question here: Why are two identical objects not equal to each other?. But it is not my question. I am aware that two identical (they are not identical actually) objects are not equal, in terms of == or ===.

My question is more about:

  • it is making more sense that: during test, what we are expecting is whether two objects are value equal not whether they are referring to the same one (reference equal)?
fluency03
  • 2,637
  • 7
  • 32
  • 62
  • 2) The object has no `obj["x.a"]` property 4) Using `deep` with only a single property (no dot) is the same as not using `deep` – Bergi Jul 29 '17 at 23:00
  • Oops, looks like I confused `deep` with `nested` in my first comment. I'm a bit confused though because [the Chai docs](http://chaijs.com/api/bdd/#method_deep) say that `expect({x: {a: 1}}).to.have.deep.property('x', {a: 1})` does indeed work. You have to use `deep` to avoid reference comparison. – Bergi Jul 30 '17 at 01:01
  • You're using ["Identical"](https://en.wikipedia.org/wiki/Identity_(object-oriented_programming)) exactly the wrong way round - it means same-reference-equal, not same-shape/value-equal. – Bergi Jul 30 '17 at 01:05
  • yes, this is my question, the Chai doc say `deep` should work in this way – fluency03 Jul 30 '17 at 10:07
  • I edited my question – fluency03 Jul 30 '17 at 10:11

0 Answers0