2

So the default pretty printer for jQuery values in jasmine isn't very pretty (it prints it as an object, listing every method available). It'd be much nicer if it just printed it as an array.

I could override jasmine.PrettyPrinter.prototype.format to give specific instructions in the case of value instanceof jQuery or override jasmine.isArray_ to return true for jQuery objects, but both of these seem like hacks.

Is there a more natural way of extending the jasmine pretty printer?

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
rampion
  • 87,131
  • 49
  • 199
  • 315

2 Answers2

1

@James deBoer solution is the right idea, but I had to modify it somewhat to get it to work:

jQuery.fn.jasmineToString = function() {
  this[0].outerHTML;
};

this is the jQuery object; [0] gives you the first DOM element (since jQuery objects are collections of matched elements), and the DOM property is outerHTML (note the capitalization).

(There's a good chance that @James has defined his own jQuery.outerHtml() function. See this StackOverflow question for more information).

Community
  • 1
  • 1
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
0

jasmine.pp will look for a method 'jasmineToString' to be defined on the object.

I extended my jQuery object like so:

jQuery.fn.jasmineToString = function() {
  return this.outerHtml();
};
James deBoer
  • 2,477
  • 15
  • 17