Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing mocha.throwError for better async error handling #985

Merged
merged 1 commit into from
Dec 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -5510,13 +5510,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

way too verbose haha var i = would do :p

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

Expand All @@ -5529,6 +5534,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 @@ -5565,6 +5571,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); }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename indexOfFn here, too? (I know this file is actually generated from the other)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah, this is the one i fixed. the other one in mocha.js is the generated one. don't bother with that one.

}
};

Expand All @@ -43,6 +48,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 @@ -79,6 +85,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);
})
})