Skip to content
Advertisement

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

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?

Advertisement

Answer

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

expect(_.some(array, { 'a': 1, 'c': 3 })).to.be.true;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement