diff --git a/spec/core/matchers/matchersUtilSpec.js b/spec/core/matchers/matchersUtilSpec.js index d6ab504e..7fd98ad4 100644 --- a/spec/core/matchers/matchersUtilSpec.js +++ b/spec/core/matchers/matchersUtilSpec.js @@ -93,6 +93,24 @@ describe("matchersUtil", function() { expect(jasmineUnderTest.matchersUtil.equals(new Error("foo"), new Error("bar"))).toBe(false); }); + it("fails for objects with different constructors", function() { + function One() {} + function Two() {} + + expect(jasmineUnderTest.matchersUtil.equals(new One(), new Two())).toBe(false); + }); + + if (typeof document === 'object') { + it("passes for equivalent objects from different frames", function() { + var iframe = document.createElement('iframe'); + document.body.appendChild(iframe); + console.log(iframe); + iframe.contentWindow.eval('window.testObject = {}'); + expect(jasmineUnderTest.matchersUtil.equals({}, iframe.contentWindow.testObject)).toBe(true); + document.body.removeChild(iframe); + }); + } + it("passes for Objects that are equivalent (simple case)", function() { expect(jasmineUnderTest.matchersUtil.equals({a: "foo"}, {a: "foo"})).toBe(true); }); diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index ef92c1d4..900c0950 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -178,8 +178,8 @@ getJasmineRequireObj().matchersUtil = function(j$) { // Objects with different constructors are not equivalent, but `Object`s // or `Array`s from different frames are. var aCtor = a.constructor, bCtor = b.constructor; - if (aCtor !== bCtor && !(isFunction(aCtor) && aCtor instanceof aCtor && - isFunction(bCtor) && bCtor instanceof bCtor)) { + if (aCtor !== bCtor && !(isObjectConstructor(aCtor) && + isObjectConstructor(bCtor))) { return false; } } @@ -239,5 +239,13 @@ getJasmineRequireObj().matchersUtil = function(j$) { function isFunction(obj) { return typeof obj === 'function'; } + + function isObjectConstructor(ctor) { + // aCtor instanceof aCtor is true for the Object and Function + // constructors (since a constructor is-a Function and a function is-a + // Object). We don't just compare ctor === Object because the constructor + // might come from a different frame with different globals. + return isFunction(ctor) && ctor instanceof ctor; + } } };