smaller, more configurable pretty-printing of objects #196

wants to merge 2 commits into


None yet
3 participants

maxbrunsfeld commented Mar 5, 2012

This includes two changes to the pretty printer:

  1. There's a new constant jasmine.MAX_PRETTY_PRINT_DEPTH, which controls how deeply the pretty-printer will recurse when stringifying an object. The depth was previously hard-coded to 40. When the limit is reached, rather than throwing an exception, the pretty printer will simply emit Object or Array. This allows you to set the recursion depth lower without breaking your tests. Personally I find that after a few levels of depth, the readability of the output decreases, so I would set MAX_PRETTY_PRINT_DEPTH to 4 or 5. Other people might want to leave it at 40 to see the entire structure.
  2. When stringifying an object, the pretty-printer will now skip over its inherited properties. This one might be controversial. We hacked it in on our project because when one of our Backbone-derived model objects got printed out, the number of prototypal methods that was included made it hard to discern much about the actual object. I think the fact that the pretty-printer currently includes these properties is responsible for most of the too-long-to-read messages that jasmine produces.

Curious what you guys think of number 2.

maxbrunsfeld added some commits Mar 5, 2012

@maxbrunsfeld maxbrunsfeld 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.
@maxbrunsfeld maxbrunsfeld Allow users to set the pretty-printer's recursion depth
Currently, jasmine's pretty printer traverses objects
to 40 levels of nesting. If an object is more deeply
nested than that, an exception is thrown. I find that
after a few levels of nesting, the output becomes
difficult to read. The process of serializing such
deep objects also sometimes crashes the browser or
causes a 'slow script' warning.

This commit exposes a 'MAX_PRETTY_PRINT_DEPTH' option.
It also causes the pretty printer to skip over
parts of an object that are nested to deeply by simply
printing out 'Object' or 'Array', rather than throwing
an exception.

ragaskar commented May 14, 2012

This seems fine to me, including #2. I have no real opinion on pretty printing depth because I've rarely seen a need for more than one or two levels to verify what the problem is with your test. We'd probably also want to knock down the default depth as part of this, 40 seems too large. Story here:

is this going to be merged or is there an alternative?


maxbrunsfeld commented Oct 28, 2012

I rebased these two commits onto master: #290. I'll close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment