samedi 29 juillet 2017

behaviour of `property` of Chai: expect({x: {a: 1}}).to.have.property('x', {a: 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. ...
  3. even for deep version, I cannot do this as well?
  4. only this is working, but this is really not as convenient and directly as the first case.

Aucun commentaire:

Enregistrer un commentaire