Automattic / expect.js

Minimalistic BDD-style assertions for Node.JS and the browser.
2.11k stars 209 forks source link

Way to Override Output Formatting #77

Open machineghost opened 10 years ago

machineghost commented 10 years ago

Currently if you do something like:

expect({foo:1}).to.be({foo:2});

you get output like:

expected {foo:1} to equal {foo:2}

That's great when you have only a single property on your object, but if your objects are full of properties (or even just have one property that's a really long string) the error output quickly becomes difficult to use.

It would be really handy if you could specify the output Expect.js should use. For instance, if the following lines:

if (value.toString) {
    return value.toString();
}

are added to "format" (inside the "i" function) then a user could do:

expect({
   foo:1
   longString: '...',
   toString: function() { 
      return 'foo ' + this.foo;
  }
}).to.be({
   foo:2
   longString: '...',
   toString: function() { 
      return 'foo ' + this.foo;
  }
});

and get the (much more useful) output:

    expected foo 1 to equal foo 2

(Of course, in a real world scenario that toString method would be defined on the class of our two objects, instead of on each object.)

Would it be possible to get something like this added to Expect.js?