Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smaller, more configurable pretty-printing of objects #196

Closed

Conversation

maxbrunsfeld
Copy link
Contributor

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.

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.
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
Copy link
Contributor

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: https://www.pivotaltracker.com/story/show/29584887

@wolframkriesing
Copy link

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

@maxbrunsfeld
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants