Chai expects: an array containing an object with at least these properties and values

I am trying to check an array of objects like:

[ { a: 1, b: 2, c: 3 }, { a: 4, b: 5, c: 6 }, ... ] 

contains at least one object with { a: 1 } and { c: 3 } :

I thought I could do it with chai-things , but I don’t know all the properties of an object that can use

 expect(array).to.include.something.that.deep.equals({ ??, a: 1, c: 3}); 

and contain.a.thing.with.property does not work with several properties: /

What is the best way to test something like this?

+7
javascript mocha chai
source share
4 answers

The most elegant solution I could come up with (using lodash):

expect (_. some (array, {'a': 1, 'c': 3})). to.be.true;

+4
source share

You can write your own function to check the array. In this example, you are passing an array and an object containing the corresponding key / value pairs:

 function deepContains(arr, search) { // first grab the keys from the search object // we'll use the length later to check to see if we can // break out of the loop var searchKeys = Object.keys(search); // loop over the array, setting our check variable to 0 for (var check = 0, i = 0; i < arr.length; i++) { var el = arr[i], keys = Object.keys(el); // loop over each array object keys for (var ii = 0; ii < keys.length; ii++) { var key = keys[ii]; // if there is a corresponding key/value pair in the // search object increment the check variable if (search[key] && search[key] === el[key]) check++; } // if we have found an object that contains a match // for the search object return from the function, otherwise // iterate again if (check === searchKeys.length) return true; } return false; } deepContains(data, { a: 4, c: 6 }); // true deepContains(data, { a: 1, c: 6 }); // false 

Demo

0
source share

The desired solution is similar to something like:

 expect(array).to.include.something.that.includes({a: 1, c: 3}); 

those. array contains an element that includes these properties. Unfortunately, chai-things is not currently supported . In foreseeable future.

After several different attempts, I found that converting the original array simplifies the task. This should work without additional libraries:

 // Get items that interest us/remove items that don't. const simplifiedArray = array.map(x => ({a: xa, c: xc})); // Now we can do a simple comparison. expect(simplifiedArray).to.deep.include({a: 1, c: 3}); 

It also allows you to scan multiple objects at once (my use case).

 expect(simplifiedArray).to.include.deep.members([{ a: 1, c: 3 }, { a: 3, c: 5 }]); 
0
source share

It looks like the chai-subset plugin from chai seems to have done the trick. Here is what I work with:

 const chai = require('chai'); const chaiSubset = require('chai-subset'); chai.use(chaiSubset); const expect = chai.expect; expect([ { type: 'text', id: 'name', name: 'name', placeholder: 'John Smith', required: 'required' }, { type: 'email', id: 'email', name: 'email', placeholder: ' example@gmail.com ', required: 'required' }, { type: 'submit' } ]).containSubset([{ type: 'text', type: 'email', type: 'submit' }]); 
0
source share

All Articles