277

Why does the following fail?

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

and what is the right way to test that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
kannix
  • 5,107
  • 6
  • 28
  • 47

7 Answers7

423

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.

moka
  • 22,846
  • 4
  • 51
  • 67
  • 2
    FTR, the deep equal syntax has changed to: `.deepEqual()` (http://chaijs.com/api/assert). – Ludder Dec 02 '14 at 09:18
  • 7
    It is not changed, you are looking at **Asserts** functionality, but not **Expect/Should** which topic starter was concerned about. – moka Dec 02 '14 at 13:53
  • 5
    You're right, I didn't read well. I thought it's just another API change. – Ludder Dec 02 '14 at 15:19
  • 6
    `to.deep.equal(['a','b'])` does not seem to work. however `to.have.all.members(['a','b'])` does. so cumbersome… – Jakob Jingleheimer Aug 10 '17 at 16:58
  • https://www.chaijs.com/plugins/deep-equal-in-any-order/ worked well when the order of object attributes was unpredictable. – asokan Feb 24 '19 at 04:18
76

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.

Meet Mehta
  • 4,839
  • 3
  • 20
  • 27
3

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

catomatic
  • 661
  • 8
  • 10
1
import chai from 'chai';
const arr1 = [2, 1];
const arr2 = [2, 1];
chai.expect(arr1).to.eql(arr2); // Will pass. `eql` is data compare instead of object compare.
Lane
  • 4,682
  • 1
  • 36
  • 20
0

You can use .deepEqual()

const { assert } = require('chai');

assert.deepEqual([0,0], [0,0]);
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
0

You can use

https://www.chaijs.com/api/assert/#method_samedeepmembers

assert.sameDeepMembers(set1, set2, [message])

Asserts that set1 and set2 have the same members in any order. Uses a deep equality check.

assert.sameDeepMembers([ { a: 1 }, { b: 2 }, { c: 3 } ], [{ b: 2 }, { a: 1 }, { c: 3 }], 'same deep members');
lejlun
  • 4,140
  • 2
  • 15
  • 31
-1

This is how to use chai to deeply test associative arrays.

I had an issue trying to assert that two associative arrays were equal. I know that these shouldn't really be used in javascript but I was writing unit tests around legacy code which returns a reference to an associative array. :-)

I did it by defining the variable as an object (not array) prior to my function call:

var myAssocArray = {};   // not []
var expectedAssocArray = {};  // not []

expectedAssocArray['myKey'] = 'something';
expectedAssocArray['differentKey'] = 'something else';

// legacy function which returns associate array reference
myFunction(myAssocArray);

assert.deepEqual(myAssocArray, expectedAssocArray,'compare two associative arrays');
GreensterRox
  • 6,432
  • 2
  • 27
  • 30