Chai - Testing for values in array of objects

后端 未结 6 1311
爱一瞬间的悲伤
爱一瞬间的悲伤 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:40

    ES6+

    Clean, functional and without dependencies, simply use a map to filter the key you want to check

    something like:

    const data = [{_id: 5, title: 'Blah', owner: 'Ted', description: 'something'},{_id: 70, title: 'GGG', owner: 'Ted', description: 'something'}];
    
    
    expect(data.map(e=>({title:e.title}))).to.include({title:"Blah"});
    

    or even shorter if you only check one key:

    expect(data.map(e=>(e.title))).to.include("Blah");
    

    https://www.chaijs.com/api/bdd/

提交回复
热议问题