Chai expect: an array to contain an object with at least these properties and values

blub picture blub · Dec 21, 2015 · Viewed 11.3k times · Source

I'm trying to validate that an array of objects like this:

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

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

I thought I could do this with chai-things, but I don't know all the properties of the object to be able to use

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

and contain.a.thing.with.property doesn't work with multiple properties :/

What's the best way to test something like this?

Answer

blub picture blub · Dec 24, 2015

Most elegant solution I could come up with (with the help of lodash):

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