0

Writing some tests and ran across an error. The array's seem the same to me, but apparently not. This is the error I'm getting. Any idea on what to do to fix it?

Expected Array [ 'A2T1511300361', 'A2T1511300362' ] to be Array [ 'A2T1511300361', 'A2T1511300362' ]

test.js

var should = require('should'), 
io = require('socket.io-client'),
path = require('path'),
express = require(path.resolve('./config/lib/express')),
mongoose = require('mongoose'),
sinon = require('sinon')   

...

client.on('printerList', function(list){

    var tempArray = [];
    tempArray.push('A2T1511300361');
    tempArray.push('A2T1511300362');
    console.log(tempArray);

    list.should.equal(tempArray);


});
Justin Young
  • 2,393
  • 3
  • 36
  • 62

2 Answers2

2

You cannot directly test array quality in the manner that you are doing. [1,2] and [1,2] may have the same elements, but they are different arrays. More formally:

[ ] !== [ ] [ ] != [ ]

You are trying to test deep equality. To do this, you need to check each array element. Many methods in lodash, for example, can help you with this. e.g.

// this uses ES6 syntax const _ = require('lodash') const arr1 = [1, 2] const arr2 = [1, 2] assert.equal(_.intersection(arr1, arr2).length, arr1.length)) assert.equal(_.intersection(arr1, arr2).length, arr2.length))

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
0

In addition to Travis's answer. Should.js also provides .eql and .deepEqual assertions which test for deep equality:

var expectedArray = [1, 2, 3];
var returnedArray = [1, 2, 3];
returnedArray.should.deepEqual(expectedArray);
Community
  • 1
  • 1
jesosk
  • 313
  • 1
  • 8