Permalink
Browse files

When pretty-printing objects, don't include inherited properties.

When making assertions about complex objects, Jasmine's
failure message are sometimes gigantic and difficult
to read because the string representation of an object
contains all of the methods and properties in its
prototype chain. This commit causes the pretty printer
to only display on object's own properties.
  • Loading branch information...
1 parent dbcb0b7 commit beeb872a82693844c58b3f28bdacccd364d8f87a @maxbrunsfeld maxbrunsfeld committed Mar 5, 2012
Showing with 9 additions and 0 deletions.
  1. +8 −0 spec/core/PrettyPrintSpec.js
  2. +1 −0 src/core/PrettyPrinter.js
@@ -32,6 +32,14 @@ describe("jasmine.pp", function () {
}, bar: [1, 2, 3]})).toEqual("{ foo : Function, bar : [ 1, 2, 3 ] }");
});
+ it("should not include inherited properties when stringifying an object", function() {
+ var SomeClass = function() {};
+ SomeClass.prototype.foo = "inherited foo";
+ var instance = new SomeClass();
+ instance.bar = "my own bar";
+ expect(jasmine.pp(instance)).toEqual("{ bar : 'my own bar' }");
+ });
+
it("should stringify RegExp objects properly", function() {
expect(jasmine.pp(/x|y|z/)).toEqual("/x|y|z/");
});
@@ -57,6 +57,7 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) {
for (var property in obj) {
+ if (!obj.hasOwnProperty(property)) continue;
if (property == '__Jasmine_been_here_before__') continue;
fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) !== jasmine.undefined &&
obj.__lookupGetter__(property) !== null) : false);

0 comments on commit beeb872

Please sign in to comment.