Add 'actual' & 'expected' property to the thrown error#34
Merged
rauchg merged 5 commits intoAutomattic:masterfrom Jun 4, 2013
Merged
Add 'actual' & 'expected' property to the thrown error#34rauchg merged 5 commits intoAutomattic:masterfrom
rauchg merged 5 commits intoAutomattic:masterfrom
Conversation
|
👍 |
|
was about to make a very similar pull request but instead of passing a 4th argument I added a new private property to the |
|
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 : {}
);
+ |
|
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);
}; |
|
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. |
|
+1 |
rauchg
added a commit
that referenced
this pull request
Jun 4, 2013
Add 'actual' & 'expected' property to the thrown error
|
Awesome, thx! 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
So mocha can display string diff.
I also fixed some unit tests that currently failing in node & browsers.