[node.js] 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?

This question is related to node.js mocha.js chai

The answer is


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.


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

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.

You can use .deepEqual()

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

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

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to mocha.js

Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Invariant Violation: Could not find "store" in either the context or props of "Connect(SportsDatabase)" Change default timeout for mocha Mocha / Chai expect.to.throw not catching thrown errors chai test array equality doesn't work as expected Code coverage with Mocha In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded How to increase timeout for a single test case in mocha How can I check that two objects have the same set of property names? How to run a single test with Mocha?

Examples related to chai

Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai Mocha / Chai expect.to.throw not catching thrown errors chai test array equality doesn't work as expected In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded How can I check that two objects have the same set of property names?