From 41a813521ba5cf407ea595c8327a63cb266833f3 Mon Sep 17 00:00:00 2001 From: Ben Christel Date: Sun, 20 Nov 2016 12:12:37 -0800 Subject: [PATCH] Fix tests for `toEqual` diff output in IE - Merges #1236 from @benchristel --- lib/jasmine-core/jasmine.js | 16 +++++++++++++--- spec/core/PrettyPrintSpec.js | 4 ++-- spec/core/matchers/toEqualSpec.js | 19 +++++++++++++++++++ src/core/matchers/matchersUtil.js | 16 +++++++++++++--- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index c462536f..1008ba5b 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -3270,14 +3270,24 @@ getJasmineRequireObj().matchersUtil = function(j$) { var aIsElement = a instanceof Element; var bIsElement = b instanceof Element; if (aIsElement && bIsElement) { - return a.outerHTML == b.outerHTML; + result = a.outerHTML == b.outerHTML; + if (!result) { + diffBuilder.record(a, b); + } + return result; } if (aIsElement || bIsElement) { + diffBuilder.record(a, b); return false; } - return a.innerText == b.innerText && a.textContent == b.textContent; + result = a.innerText == b.innerText && a.textContent == b.textContent; + if (!result) { + diffBuilder.record(a, b); + } + return result; } if (aIsDomNode || bIsDomNode) { + diffBuilder.record(a, b); return false; } @@ -3444,7 +3454,7 @@ getJasmineRequireObj().matchersUtil = function(j$) { return 'Expected ' + path + ' to be a kind of ' + - expected.constructor.name + + j$.fnNameFor(expected.constructor) + ', but was ' + j$.pp(actual) + '.'; } diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index 3965ea73..0656f902 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -100,8 +100,8 @@ describe("jasmineUnderTest.pp", function () { }); it("should print 'null' as the constructor of an object with its own constructor property", function() { - expect(jasmineUnderTest.pp({constructor: function() {}})).toEqual("null({ constructor: Function })"); - expect(jasmineUnderTest.pp({constructor: 'foo'})).toEqual("null({ constructor: 'foo' })"); + expect(jasmineUnderTest.pp({constructor: function() {}})).toContain("null({"); + expect(jasmineUnderTest.pp({constructor: 'foo'})).toContain("null({"); }); it("should not include inherited properties when stringifying an object", function() { diff --git a/spec/core/matchers/toEqualSpec.js b/spec/core/matchers/toEqualSpec.js index 2ac0a367..88ca9aec 100644 --- a/spec/core/matchers/toEqualSpec.js +++ b/spec/core/matchers/toEqualSpec.js @@ -289,7 +289,22 @@ describe("toEqual", function() { expect(compareEquals(actual, expected).message).toEqual(message); }); + function constructorIsNotEnumerable() { + // in IE8, the constructor property is not enumerable, even if it is an + // own property of the object. + // Objects that differ only by an own `constructor` property are thus + // considered equal in IE8. + for (var key in {constructor: 1}) { + return false; + } + return true; + } + it("reports mismatches between objects with their own constructor property", function () { + if (constructorIsNotEnumerable()) { + return; + } + function Foo() {} function Bar() {} @@ -301,6 +316,10 @@ describe("toEqual", function() { }); it("reports mismatches between an object with a real constructor and one with its own constructor property", function () { + if (constructorIsNotEnumerable()) { + return; + } + function Foo() {} function Bar() {} diff --git a/src/core/matchers/matchersUtil.js b/src/core/matchers/matchersUtil.js index 5998c393..28314891 100644 --- a/src/core/matchers/matchersUtil.js +++ b/src/core/matchers/matchersUtil.js @@ -189,14 +189,24 @@ getJasmineRequireObj().matchersUtil = function(j$) { var aIsElement = a instanceof Element; var bIsElement = b instanceof Element; if (aIsElement && bIsElement) { - return a.outerHTML == b.outerHTML; + result = a.outerHTML == b.outerHTML; + if (!result) { + diffBuilder.record(a, b); + } + return result; } if (aIsElement || bIsElement) { + diffBuilder.record(a, b); return false; } - return a.innerText == b.innerText && a.textContent == b.textContent; + result = a.innerText == b.innerText && a.textContent == b.textContent; + if (!result) { + diffBuilder.record(a, b); + } + return result; } if (aIsDomNode || bIsDomNode) { + diffBuilder.record(a, b); return false; } @@ -363,7 +373,7 @@ getJasmineRequireObj().matchersUtil = function(j$) { return 'Expected ' + path + ' to be a kind of ' + - expected.constructor.name + + j$.fnNameFor(expected.constructor) + ', but was ' + j$.pp(actual) + '.'; }