Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 490
Alissa Avatar asked Nov 16 '15 15:11

Alissa


People also ask

How do you compare two objects in Chai?

You can use deep-equal-in-any-order plugin. Chai plugin to match objects and arrays deep equality with arrays (including nested ones) being in any order. It works in a similar way as deep. equal but it doesn't check the order of the arrays (at any level of nested objects and arrays).

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do I compare to arrays JavaScript?

While JavaScript does not have an inbuilt method to directly compare two arrays, it does have inbuilt methods to compare two strings. Strings can also be compared using the equality operator. Therefore, we can convert the arrays to strings, using the Array join() method, and then check if the strings are equal.


Video Answer


2 Answers

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. :-)

like image 196
sindrenm Avatar answered Oct 09 '22 09:10

sindrenm


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.

like image 42
Magus Avatar answered Oct 09 '22 09:10

Magus