chai test array equality doesn't work as expected

node.jsmocha.jsChai

node.js Problem Overview


Why does the following fail?

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

and what is the right way to test that?

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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.

Solution 3 - node.js

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

Solution 4 - node.js

You can use .deepEqual()

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

assert.deepEqual([0,0], [0,0]);

Solution 5 - node.js

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.

Solution 6 - node.js

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');

Solution 7 - node.js

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');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkannixView Question on Stackoverflow
Solution 1 - node.jsmokaView Answer on Stackoverflow
Solution 2 - node.jsMeet MehtaView Answer on Stackoverflow
Solution 3 - node.jscatomaticView Answer on Stackoverflow
Solution 4 - node.jsDmitry GrinkoView Answer on Stackoverflow
Solution 5 - node.jsLaneView Answer on Stackoverflow
Solution 6 - node.jsGianluca TomasinoView Answer on Stackoverflow
Solution 7 - node.jsGreensterRoxView Answer on Stackoverflow