Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chai test array equality doesn't work as expected

Why does the following fail?

expect([0,0]).to.equal([0,0]);

and what is the right way to test that?

like image 609
kannix Avatar asked Jul 08 '13 12:07

kannix


3 Answers

For expect, .equal will compare objects rather than their data, and in your case it is two different arrays.

Use .eql in order to deeply compare values. Check out this link.
Or you could use .deep.equal in order to simulate same as .eql.
Or in your case you might want to check .members.

For asserts you can use .deepEqual, link.

like image 150
moka Avatar answered Oct 19 '22 19:10

moka


Try to use deep Equal. It will compare nested arrays as well as nested Json.

expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });

Please refer to main documentation site.

like image 73
Meet Mehta Avatar answered Oct 19 '22 19:10

Meet Mehta


for unordered deep equality, use members

expect([1,2,3]).to.have.members([3,2,1]); // passes expect([1,2,3]).to.have.members([1,2,3]); // passes expect([1,2,3]).to.eql([3,2,1]); // fails

source

like image 3
catomatic Avatar answered Oct 19 '22 21:10

catomatic