Justin Young Justin Young - 2 years ago 103
Node.js Question

Expect array to be array

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' ]


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 = [];



Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download