Jasmine.js comparing arrays Jasmine.js comparing arrays javascript javascript

Jasmine.js comparing arrays


Just did the test and it works with toEqual

please find my test:

http://jsfiddle.net/7q9N7/3/

describe('toEqual', function() {    it('passes if arrays are equal', function() {        var arr = [1, 2, 3];        expect(arr).toEqual([1, 2, 3]);    });});

Just for information:

toBe() versus toEqual(): toEqual() checks equivalence. toBe(), on the other hand, makes sure that they're the exact same object.


You can compare an array like the below mentioned if the array has some values

it('should check if the array are equal', function() {        var mockArr = [1, 2, 3];        expect(mockArr ).toEqual([1, 2, 3]); });

But if the array that is returned from some function has more than 1 elements and all are zero then verify by using

expect(mockArray[0]).toBe(0);


just for the record you can always compare using JSON.stringify

const arr = [1,2,3];expect(JSON.stringify(arr)).toBe(JSON.stringify([1,2,3]));expect(JSON.stringify(arr)).toEqual(JSON.stringify([1,2,3]));

It's all meter of taste, this will also work for complex literal objects