Skip to content
This repository
Browse code

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...
commit beeb872a82693844c58b3f28bdacccd364d8f87a 1 parent dbcb0b7
maxbrunsfeld authored March 04, 2012
8  spec/core/PrettyPrintSpec.js
@@ -32,6 +32,14 @@ describe("jasmine.pp", function () {
32 32
     }, bar: [1, 2, 3]})).toEqual("{ foo : Function, bar : [ 1, 2, 3 ] }");
33 33
   });
34 34
 
  35
+  it("should not include inherited properties when stringifying an object", function() {
  36
+    var SomeClass = function() {};
  37
+    SomeClass.prototype.foo = "inherited foo";
  38
+    var instance = new SomeClass();
  39
+    instance.bar = "my own bar";
  40
+    expect(jasmine.pp(instance)).toEqual("{ bar : 'my own bar' }");
  41
+  });
  42
+
35 43
   it("should stringify RegExp objects properly", function() {
36 44
     expect(jasmine.pp(/x|y|z/)).toEqual("/x|y|z/");
37 45
   });
1  src/core/PrettyPrinter.js
@@ -57,6 +57,7 @@ jasmine.PrettyPrinter.prototype.format = function(value) {
57 57
 
58 58
 jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) {
59 59
   for (var property in obj) {
  60
+    if (!obj.hasOwnProperty(property)) continue;
60 61
     if (property == '__Jasmine_been_here_before__') continue;
61 62
     fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) !== jasmine.undefined && 
62 63
                                          obj.__lookupGetter__(property) !== null) : false);

0 notes on commit beeb872

Please sign in to comment.
Something went wrong with that request. Please try again.