Chai - Testing for values in array of objects

53,229

Solution 1

This is what I usually do within the test:

var result = query_result;

var members = [];
result.forEach(function(e){
    members.push(e.title);
});

expect(members).to.have.members(['expected_title_1','expected_title_2']);

If you know the order of the return array you could also do this:

expect(result).to.have.deep.property('[0].title', 'expected_title_1');
expect(result).to.have.deep.property('[1].title', 'expected_title_2');

Solution 2

As stated here following code works now with [email protected] and chai-things. I just love the natural readability of this approach.

var chai = require('chai'),
    expect = chai.expect;

chai.use(require('chai-like'));
chai.use(require('chai-things')); // Don't swap these two

expect(data).to.be.an('array').that.contains.something.like({title: 'Blah'});

Solution 3

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

Solution 4

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/

Solution 5

Here is another approach that I found to be more helpful. Basically, use string interpolation and map your array of objects to an array of string literals. Then you can write expectations against the array of strings.

const locations: GeoPoint[] = [
  {
    latitude: 10,
    longitude: 10
  },
  {
    latitude: 9,
    longitude: 9
  },
  {
    latitude: -10,
    longitude: -10
  },
  {
    latitude: -9,
    longitude: -9
  }
];
const stringLocations: string[] = locations.map((val: GeoPoint) => 
  `${val.latitude},${val.longitude}`
);

expect(stringLocations).to.contain('-9.5,-9.5');
expect(stringLocations).to.contain('9.5,9.5');
Share:
53,229
Admin
Author by

Admin

Updated on November 29, 2021

Comments

  • Admin
    Admin over 2 years

    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', description: 'something'...},
     {_id: 70, title: 'GGG', owner: 'Ted', description: 'something'...}...]
    

    What I want my tests to verify is that in the return array it conatins the specific titles that should return. Nothing I do using Chai/Chai-Things seems to work. Things like res.body.savedResults.should.include.something.that.equals({title: 'Blah'}) error out I'm assuming since the record object contains other keys and values besides just title.

    Is there a way to make it do what I want? I just need to verify that the titles are in the array and don't care what the other data might be (IE _id).

    Thanks