Comparing arrays in chai

You can do it with 2 lines :

expect(listFromPage).to.eventually.include.all.members(predefinedArray)
expect(predefinedArray).to.eventually.include.all.members(listFromPage)

With this, you'll check if both arrays contains the same values. But order does not matter.


Seeing as this was marked as resolved earlier, I tried doing the same thing as in the accepted answer. It probably worked back then, but doesn't seem to work anymore:

expect([1, 2, 3, 4]).to.have.all.members([2, 4, 3, 1]);

Gives the following error:

AssertionError: expected 1 to be an array

I did a little more research and found a pull request that added this functionality back in 2013:

https://github.com/chaijs/chai/pull/153

So the official way of doing this now is like this:

expect([1, 2, 3, 4]).to.have.same.members([2, 4, 3, 1]);

For completeness, here's the error that two different sets produces:

AssertionError: expected [ 1, 2, 3, 4 ] to have the same members as [ 4, 3, 1 ]

Hope this helps anyone searching for the same answer now. :-)


From the future, the way that worked for me, was to use .deepEqual which did the trick for me

assert.deepEqual(['name'], ['name'], 'this must be same to proceed');

It's not entirely clear from the documentation, but .to.have.all.members seems to work. I could only find a mention of this feature for .keys, but looks like it also works for .members with arrays.