Alissa Alissa -4 years ago 178
Node.js Question

Comparing arrays in chai

I'm writing some tests with chai and chai-as-promised (and more frameworks, but it doesn't matter in this case) and I need to check if array I get from a web-page is same as a predefined array. I tried to use

expect(arrayFromPage).to.eventually.deep.equal(predefinedArray)
, but it won't work, because order of elements on page is sometimes different (which is OK, I don't need to check if they are in the same order).

I've found a way to workaround the issue by using
expect(listFromPage).to.eventually.include.all.members(predefinedArray)
, but I'd like to know if there is a better solution.

What bothers me most in my workaround, is that I only assure that
predefinedArray
is subset of
listFromPage
, not that they are made of same elements.

So, I'd like to know if there is an assert that will pass for
[1,2,3]
and
[3,2,1]
, but not for
[1]
and
[1,2,3]
or
[1,2,3,4]
and
[1,2,3]
.

I know that I can use some second expectation (compare lengths, or something else), but I'd like to know if there is a one-line solution.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download