After learning about the differences between the comparison operators === and == in Javascript I was pretty surprised that Jasmines equality concept does not correspond to either one. For example all statements in the following test suite are true:
describe('Jasmine asserted equality of objects', function() {
it('is different from ===- and ==-equality', function() {
var x = {};
var y = {};
expect(x === y).toBeFalsy();
expect(x == y).toBeFalsy();
expect(x).toEqual(y);
});
it('seems to imply element-wise equality', function() {
var x = {'name': 'value'};
var y1 = {'name': 'values'};
var y2 = {'names': 'value'};
var y3 = {'name': 'value'};
expect(x).not.toEqual(y1);
expect(x).not.toEqual(y2);
expect(x).toEqual(y3);
});
it('does not imply equality of the respective prototypes', function() {
var x = {};
var y = Object.create(x);
var pr = Object.getPrototypeOf;
expect(x).toEqual(y);
expect(pr(pr(x))).not.toEqual(pr(pr(y)));
});
});
Unfortunately, I was not able to find any official API documentation for Jasmine. The well known introduction page only provides examples, and most other sources I found either discuss the difference of === and == or Jasmines concept of custom matchers.
However, my main question is the following: How is Jasmines function toEqual
specified? In addition I'd be interested in the reasoning for introducing this new concept of equality: Why or in which cases is expect(x).toEqual(y)
better suited than x === y
(or x == y
)?