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

Merged
merged 5 commits into from Jun 4, 2013

Conversation

Projects
None yet
7 participants
@curvedmark

So mocha can display string diff.

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

@curvedmark curvedmark referenced this pull request in mochajs/mocha Sep 27, 2012

Closed

Add object diff #589

@jhnns

This comment has been minimized.

Show comment Hide comment
@jhnns

jhnns Oct 26, 2012

👍

jhnns commented Oct 26, 2012

👍

@doctyper

This comment has been minimized.

Show comment Hide comment
@doctyper

doctyper Oct 26, 2012

+1

+1

@hurrymaplelad

This comment has been minimized.

Show comment Hide comment
@hurrymaplelad

hurrymaplelad Nov 25, 2012

+1

@millermedeiros

This comment has been minimized.

Show comment Hide comment
@millermedeiros

millermedeiros Dec 5, 2012

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;

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

This comment has been minimized.

Show comment Hide comment
@millermedeiros

millermedeiros Dec 7, 2012

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 : {}
 );
+

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

This comment has been minimized.

Show comment Hide comment
@millermedeiros

millermedeiros Jan 9, 2013

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);
};

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

This comment has been minimized.

Show comment Hide comment
@jhnns

jhnns 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.

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

This comment has been minimized.

Show comment Hide comment
@switz

switz Feb 25, 2013

+1

switz commented Feb 25, 2013

+1

rauchg added a commit that referenced this pull request Jun 4, 2013

Merge pull request #34 from curvedmark/mocha-diff
Add 'actual' & 'expected' property to the thrown error

@rauchg rauchg merged commit f62fd59 into Automattic:master Jun 4, 2013

@jhnns

This comment has been minimized.

Show comment Hide comment
@jhnns

jhnns Jun 5, 2013

Awesome, thx! 👍

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