Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improve assertion messages in uncaught exceptions #172

Merged
merged 1 commit into from

2 participants

@danielbeardsley

Wrap all assert.* methods in a try/catch that appends the
current test to the Error object and rethrows.

This allows us to get the corresponding Test object in the
uncaughtException event.

Before:

uncaught undefined: AssertionError: 'blah' === 'blahs'
 at Blah.<anonymous> (/home/user/test.js:15:17)
 ...

After:

test.js testing blah(): AssertionError: 'blah' === 'blahs'
 at Blah.<anonymous> (/home/user/test.js:15:17)
 ...
@danielbeardsley danielbeardsley Improve assertion messages in uncaught exceptions
Wrap all assert.* methods in a try/catch that appends the
current test to the Error object and rethrows.

This allows us to get the corresponding Test object in the
`uncaughtException` event.

Before:

   uncaught undefined: AssertionError: 'blah' === 'blahs'
    at Blah.<anonymous> (/home/user/test.js:15:17)
    ...

After:

   test.js testing blah(): AssertionError: 'blah' === 'blahs'
    at Blah.<anonymous> (/home/user/test.js:15:17)
    ...
fc43ba6
@deralex deralex was assigned
@deralex deralex merged commit f6e29e1 into visionmedia:master
@deralex deralex was unassigned by danielbeardsley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2012
  1. @danielbeardsley

    Improve assertion messages in uncaught exceptions

    danielbeardsley authored
    Wrap all assert.* methods in a try/catch that appends the
    current test to the Error object and rethrows.
    
    This allows us to get the corresponding Test object in the
    `uncaughtException` event.
    
    Before:
    
       uncaught undefined: AssertionError: 'blah' === 'blahs'
        at Blah.<anonymous> (/home/user/test.js:15:17)
        ...
    
    After:
    
       test.js testing blah(): AssertionError: 'blah' === 'blahs'
        at Blah.<anonymous> (/home/user/test.js:15:17)
        ...
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 4 deletions.
  1. +21 −4 bin/expresso
  2. +8 −0 test/local-assert.test.js
View
25 bin/expresso
@@ -550,6 +550,24 @@ assert.response = function(server, req, res, msg) {
};
/**
+ * Custom assert module that tacks on the current test to every error
+ */
+var customAssert = { __proto__: assert };
+Object.keys(assert).forEach(function (method) {
+ customAssert[method] = function () {
+ try {
+ return assert[method].apply(assert, arguments);
+ } catch (err) {
+ // 'this' is this assert object from the test
+ if (this._test) {
+ err._test = this._test;
+ }
+ throw err;
+ }
+ };
+});
+
+/**
* Pad the given string to the maximum width provided.
*
* @param {String} str
@@ -885,7 +903,7 @@ function Test(options) {
this._failed = [];
this._pending = [];
this._beforeExit = [];
- this.assert = { __proto__: assert, _test: this };
+ this.assert = { __proto__: customAssert, _test: this };
var test = this;
process.on('beforeExit', function() {
@@ -976,11 +994,10 @@ Test.prototype.runParallel = function() {
Test.prototype.error = function(err) {
if (!err._reported) {
++failures;
+ var test = err._test || this;
var name = err.name,
stack = err.stack ? err.stack.replace(err.name, '') : '',
- label = this.title === 'uncaught'
- ? this.title
- : this.suite + ' ' + this.title;
+ label = test.suite + ' ' + test.title;
print('\n [bold]{' + label + '}: [red]{' + name + '}' + stack + '\n');
err._reported = true;
}
View
8 test/local-assert.test.js
@@ -5,6 +5,14 @@ exports['first'] = function(beforeExit, assert) {
process.nextTick(function() {
assert.equal(assert._test.suite, 'local-assert.test.js');
assert.equal(assert._test.title, 'first');
+ try {
+ var error = false;
+ assert.ok(false);
+ } catch (err) {
+ assert.equal(err._test, assert._test);
+ error = true;
+ }
+ assert.ok(error);
});
};
Something went wrong with that request. Please try again.