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

, 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
, 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
is subset of
, not that they are made of same elements.

So, I'd like to know if there is an assert that will pass for
, but not for

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