1

enter image description here

My test, below the alerts var is an empty array [] however this throws an error.

it('FeedFactory alerts array is filled', function() {
    var alerts = FeedFactory.returnAlerts();
    console.log('alerts',alerts);
    expect(alerts).to.equal([]);
});

Here is the test log when I comment out the last expect(alerts).to.equal([]); line.

enter image description here

Code from my Factory

var alerts = [];

var feedFactory = {
    alerts       : alerts,
    getFeed      : getFeed,
    returnAlerts : returnAlerts,
    saveFilters  : saveFilters,
    getFilters   : getFilters
};

function returnAlerts() {
    return alerts;
}
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

2 Answers2

8

Because [] !== [] && [] != []. JS objects are not loosely or strictly equal to one another, so expect([]).to.equal([]) will always fail.

You should use a deeper form of equality, such as deep (or its confusing shorthand, eql) or members:

expect([1, 2, 3]).to.deep.equal([1, 2, 3])
expect([1, 2, 3]).to.include.members([1, 2])
ssube
  • 47,010
  • 7
  • 103
  • 140
2

Two empty arrays are not equal to each other because each has a different reference, since all objects, (and arrays for that matter) are referenced and not copied. The checking for equality on objects are checking on reference and not internal content

hance:

var a = [];
var b = [];
console.log(a == b) //false

i would use the length of the alerts array inorder to test if its an empty array. by writing:

expect(alerts.length).to.equal(0);

in order to check the content of the array to equal another content of another array, you must use deep checking, which checks for each value of the array to equal the one in the other array (quite expensive).

Bamieh
  • 10,358
  • 4
  • 31
  • 52