diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 5e291318..66e56f95 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1739,6 +1739,12 @@ getJasmineRequireObj().pp = function(j$) { this.seen = []; } + function hasCustomToString(value) { + // value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g. + // iframe, web worker) + return value.toString !== Object.prototype.toString && (value.toString() !== Object.prototype.toString.call(value)); + } + PrettyPrinter.prototype.format = function(value) { this.ppNestLevel_++; try { @@ -1764,7 +1770,7 @@ getJasmineRequireObj().pp = function(j$) { this.emitScalar('HTMLNode'); } else if (value instanceof Date) { this.emitScalar('Date(' + value + ')'); - } else if (value.toString && typeof value === 'object' && !(value instanceof Array) && value.toString !== Object.prototype.toString) { + } else if (value.toString && typeof value === 'object' && !j$.isArray_(value) && hasCustomToString(value)) { this.emitScalar(value.toString()); } else if (j$.util.arrayContains(this.seen, value)) { this.emitScalar(''); diff --git a/spec/core/PrettyPrintSpec.js b/spec/core/PrettyPrintSpec.js index fcaab1e6..212337a3 100644 --- a/spec/core/PrettyPrintSpec.js +++ b/spec/core/PrettyPrintSpec.js @@ -187,6 +187,15 @@ describe("jasmineUnderTest.pp", function () { }; expect(jasmineUnderTest.pp(obj)).toEqual("my toString"); + + // Simulate object from another global context (e.g. an iframe or Web Worker) that does not actually have a custom + // toString despite obj.toString !== Object.prototype.toString + var objFromOtherContext = { + foo: 'bar', + toString: function () { return Object.prototype.toString.call(this); } + }; + + expect(jasmineUnderTest.pp(objFromOtherContext)).toEqual("Object({ foo: 'bar', toString: Function })"); }); it("should stringify objects from anonymous constructors with custom toString", function () { diff --git a/src/core/PrettyPrinter.js b/src/core/PrettyPrinter.js index e9452961..e6683fa7 100644 --- a/src/core/PrettyPrinter.js +++ b/src/core/PrettyPrinter.js @@ -5,6 +5,12 @@ getJasmineRequireObj().pp = function(j$) { this.seen = []; } + function hasCustomToString(value) { + // value.toString !== Object.prototype.toString if value has no custom toString but is from another context (e.g. + // iframe, web worker) + return value.toString !== Object.prototype.toString && (value.toString() !== Object.prototype.toString.call(value)); + } + PrettyPrinter.prototype.format = function(value) { this.ppNestLevel_++; try { @@ -30,7 +36,7 @@ getJasmineRequireObj().pp = function(j$) { this.emitScalar('HTMLNode'); } else if (value instanceof Date) { this.emitScalar('Date(' + value + ')'); - } else if (value.toString && typeof value === 'object' && !(value instanceof Array) && value.toString !== Object.prototype.toString) { + } else if (value.toString && typeof value === 'object' && !j$.isArray_(value) && hasCustomToString(value)) { this.emitScalar(value.toString()); } else if (j$.util.arrayContains(this.seen, value)) { this.emitScalar('');