Chai - Testing for values in array of objects

后端 未结 6 1275
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 12:22

I am setting up my tests for the results to a REST endpoint that returns me an array of Mongo database objects.

[{_id: 5, title: \'Blah\', owner: \'Ted\', de         


        
6条回答
  •  被撕碎了的回忆
    2021-01-01 12:57

    Probably the best way now a days would be to use deep.members property

    This checks for unordered complete equality. (for incomplete equality change members for includes)

    i.e.

    expect([ {a:1} ]).to.have.deep.members([ {a:1} ]); // passes
    expect([ {a:1} ]).to.have.members([ {a:1} ]); // fails
    

    Here is a great article on testing arrays and objects https://medium.com/building-ibotta/testing-arrays-and-objects-with-chai-js-4b372310fe6d

    DISCLAIMER: this is to not only test the title property, but rather a whole array of objects

提交回复
热议问题