Skip to content

Commit

Permalink
Merge pull request mochajs#985 from jpbochi/async-error-handling
Browse files Browse the repository at this point in the history
Introducing mocha.throwError for better async error handling

* jpbochi/async-error-handling:
  introduced mocha.throwError for better async error handling
  • Loading branch information
travisjeffery committed Dec 7, 2013
2 parents b9b7355 + 9e652eb commit e7364f3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
20 changes: 19 additions & 1 deletion mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -5569,13 +5569,18 @@ var process = {};
process.exit = function(status){};
process.stdout = {};

var uncaughtExceptionHandlers = [];

/**
* Remove uncaughtException listener.
*/

process.removeListener = function(e){
process.removeListener = function(e, fn){
if ('uncaughtException' == e) {
global.onerror = function() {};

var indexOfFn = uncaughtExceptionHandlers.indexOf(fn);
if (indexOfFn != -1) { uncaughtExceptionHandlers.splice(indexOfFn, 1); }
}
};

Expand All @@ -5588,6 +5593,7 @@ process.on = function(e, fn){
global.onerror = function(err, url, line){
fn(new Error(err + ' (' + url + ':' + line + ')'));
};
uncaughtExceptionHandlers.push(fn);
}
};

Expand Down Expand Up @@ -5624,6 +5630,18 @@ Mocha.Runner.immediately = function(callback) {
}
};

/**
* Function to allow assertion libraries to throw errors directly into mocha.
* This is useful when running tests in a browser because window.onerror will
* only receive the 'message' attribute of the Error.
*/
mocha.throwError = function(err) {
uncaughtExceptionHandlers.forEach(function (fn) {
fn(err);
});
throw err;
};

/**
* Override ui to ensure that the ui functions are initialized.
* Normally this would happen in Mocha.prototype.loadFiles.
Expand Down
20 changes: 19 additions & 1 deletion support/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ var process = {};
process.exit = function(status){};
process.stdout = {};

var uncaughtExceptionHandlers = [];

/**
* Remove uncaughtException listener.
*/

process.removeListener = function(e){
process.removeListener = function(e, fn){
if ('uncaughtException' == e) {
global.onerror = function() {};

var indexOfFn = uncaughtExceptionHandlers.indexOf(fn);
if (indexOfFn != -1) { uncaughtExceptionHandlers.splice(indexOfFn, 1); }
}
};

Expand All @@ -44,6 +49,7 @@ process.on = function(e, fn){
fn(new Error(err + ' (' + url + ':' + line + ')'));
return true;
};
uncaughtExceptionHandlers.push(fn);
}
};

Expand Down Expand Up @@ -85,6 +91,18 @@ Mocha.Runner.immediately = function(callback) {
}
};

/**
* Function to allow assertion libraries to throw errors directly into mocha.
* This is useful when running tests in a browser because window.onerror will
* only receive the 'message' attribute of the Error.
*/
mocha.throwError = function(err) {
uncaughtExceptionHandlers.forEach(function (fn) {
fn(err) ;
});
throw err;
};

/**
* Override ui to ensure that the ui functions are initialized.
* Normally this would happen in Mocha.prototype.loadFiles.
Expand Down
17 changes: 17 additions & 0 deletions test/browser/large.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,21 @@ describe('something', function(){
done();
}, 1);
})

it('should provide an even better error on phantomjs', function(done){
setTimeout(function(){
var AssertionError = function(message, actual, expected) {
this.message = message;
this.actual = actual;
this.expected = expected;
this.showDiff = true;
};
AssertionError.prototype = Object.create(Error.prototype);
AssertionError.prototype.name = 'AssertionError';
AssertionError.prototype.constructor = AssertionError;

mocha.throwError(new AssertionError('kabooom', 'text with a typo', 'text without a typo'));
done();
}, 1);
})
})

0 comments on commit e7364f3

Please sign in to comment.