Why jasmineToString instead of toString?

524 views
Skip to first unread message

André Willik Valenti

unread,
Dec 11, 2013, 7:57:30 AM12/11/13
to jasmine...@googlegroups.com
Hello,

I've been using Jasmine for a year or so and I'm very satisfied with it.

Recently, I was having a hard time debugging my objects, since their string representations were too big. I tried implementing toString, but it didn't work.

I was willing to implement this feature myself in Jasmine. So, I started looking at Jasmine source code, but then I found a spec describing this feature as actually already implemented, only that it expects a method named jasmineToString(), instead of toString() (see here, at the end of the file: https://github.com/pivotal/jasmine/blob/1_3_x/spec/core/PrettyPrintSpec.js)

Many people might go through this same problem, never finding out the solution. Besides, creating a method with such name in one's objects/classes will artificially couple one's code to Jasmine. Why not the usual toString? As far as I know, debugging is its sole purpose, at least in Java. I'm unable to see any reason not to use. Is there any?

Regards,
André

Gregg Van Hove

unread,
Dec 11, 2013, 10:10:15 AM12/11/13
to jasmine...@googlegroups.com
The original intent was not to collide with the built in toString in the case where custom output was desired only for use in tests. Additionally, this makes it totally clear that this method is for use in jasmine tests and you probably won't find any usages in your own code. 

The custom method name also makes it easy to check if a particular object has it defined, since all objects have a toString method. 

That said, I think we would be open to hanging what we look for when pretty printing. And we're looking at reworking how the pretty printer is implemented soon. 

- Gregg
--
You received this message because you are subscribed to the Google Groups "Jasmine Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js-de...@googlegroups.com.
To post to this group, send email to jasmine...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js-dev.
For more options, visit https://groups.google.com/groups/opt_out.


--

-Gregg

André Willik Valenti

unread,
Dec 11, 2013, 1:10:25 PM12/11/13
to jasmine...@googlegroups.com
I agree that users should be able to implement jasmineToString if they like. But, in case they don't implement it, shouldn't their custom toString be preferred over the recursive pretty print? At least in case toString returns something different from '[object Object]'.

Maybe this code would do the job:

if (object.toString() !== '[object Object]') {
  this.emitScalar(value.toString());
}

or maybe:

if (object.toString !== Object.prototype.toString) {  // Still doesn't work for Arrays, but does work for most cases
  this.emitScalar(value.toString());
}



2013/12/11 Gregg Van Hove <gvan...@pivotallabs.com>
Reply all
Reply to author
Forward
0 new messages