chai test array equality doesn't work as expected

kannix picture kannix · Jul 8, 2013 · Viewed 102.3k times · Source

Why does the following fail?

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

and what is the right way to test that?

Answer

moka picture moka · Jul 8, 2013

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.