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:
- why I cannot directly put
{a: 1}
as the value of propertyx
? - the object has no obj["x.a"] property
- using deep with only a single property (no dot) is the same as not using deep
- 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)?