Skip to content

Add 'actual' & 'expected' property to the thrown error#34

Merged
rauchg merged 5 commits intoAutomattic:masterfrom
curvedmark:mocha-diff
Jun 4, 2013
Merged

Add 'actual' & 'expected' property to the thrown error#34
rauchg merged 5 commits intoAutomattic:masterfrom
curvedmark:mocha-diff

Conversation

@curvedmark
Copy link
Copy Markdown

So mocha can display string diff.

I also fixed some unit tests that currently failing in node & browsers.

@jhnns
Copy link
Copy Markdown

jhnns commented Oct 26, 2012

👍

2 similar comments
@doctyper
Copy link
Copy Markdown

+1

@hurrymaplelad
Copy link
Copy Markdown

+1

@millermedeiros
Copy link
Copy Markdown

was about to make a very similar pull request but instead of passing a 4th argument I added a new private property to the Assertion - this._expected = obj;

@millermedeiros
Copy link
Copy Markdown

just to document my changes (which are simpler BTW):

diff --git a/expect.js b/expect.js
index 58c7049..8c08191 100644
--- a/expect.js
+++ b/expect.js
@@ -96,7 +96,12 @@
       , ok = this.flags.not ? !truth : truth;

     if (!ok) {
-      throw new Error(msg.call(this));
+      var err = new Error(msg.call(this));
+      if ('_expected' in this) {
+        err.expected = this._expected;
+        err.actual = this.obj;
+      }
+      throw err;
     }

     this.and = new Assertion(this.obj);
@@ -197,6 +202,7 @@

   Assertion.prototype.be =
   Assertion.prototype.equal = function (obj) {
+    this._expected = obj;
     this.assert(
         obj === this.obj
       , function(){ return 'expected ' + i(this.obj) + ' to equal ' + i(obj) }
@@ -211,6 +217,7 @@
    */

   Assertion.prototype.eql = function (obj) {
+    this._expected = obj;
     this.assert(
         expect.eql(obj, this.obj)
       , function(){ return 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj) }
@@ -1251,3 +1258,4 @@
   , 'undefined' != typeof module ? module : {}
   , 'undefined' != typeof exports ? exports : {}
 );
+

@millermedeiros
Copy link
Copy Markdown

any news about this issue?

I ended up monkey-patching expect.js before running any of the tests, that way I avoid maintaining my own fork and make clear that I edited the code:

// monkey-patch expect.js for better diffs on mocha
// see: https://github.com/LearnBoost/expect.js/pull/34

var expect = require('expect.js');

var origBe = expect.Assertion.prototype.be;
expect.Assertion.prototype.be =
expect.Assertion.prototype.equal = function(obj){
    this._expected = obj;
    origBe.call(this, obj);
};

expect.Assertion.prototype.assert = function (truth, msg, error) {
    msg = this.flags.not ? error : msg;
    var ok = this.flags.not ? !truth : truth;
    if (!ok) {
        var err = new Error(msg.call(this));
        if ('_expected' in this) {
            err.expected = this._expected;
            err.actual = this.obj;
        }
        throw err;
    }
    this.and = new expect.Assertion(this.obj);
};

@jhnns
Copy link
Copy Markdown

jhnns commented Jan 9, 2013

Since mocha is a popular test runner, this should really be part of expect.js. I'm tired of starring at the console to find the difference.

@switz
Copy link
Copy Markdown

switz commented Feb 25, 2013

+1

rauchg added a commit that referenced this pull request Jun 4, 2013
Add 'actual' & 'expected' property to the thrown error
@rauchg rauchg merged commit f62fd59 into Automattic:master Jun 4, 2013
@jhnns
Copy link
Copy Markdown

jhnns commented Jun 5, 2013

Awesome, thx! 👍

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.

7 participants