☕️ Classic, reliable, trusted test framework for Node.js and the browser
introduced mocha.throwError for better async error handling
JP Bochi9e652eb9f24a
diff --git a/mocha.js b/mocha.jsindex d11f3419..58b4d094 100644--- a/mocha.js+++ b/mocha.js@@ -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);+ if (indexOfFn != -1) { uncaughtExceptionHandlers.splice(indexOfFn, 1); }}};@@ -5529,6 +5534,7 @@ process.on = function(e, fn){global.onerror = function(err, url, line){fn(new Error(err + ' (' + url + ':' + line + ')'));};+ uncaughtExceptionHandlers.push(fn);}};@@ -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.diff --git a/support/tail.js b/support/tail.jsindex 4dcdebe0..3f1193a1 100644--- a/support/tail.js+++ b/support/tail.js@@ -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); }}};@@ -43,6 +48,7 @@ process.on = function(e, fn){global.onerror = function(err, url, line){fn(new Error(err + ' (' + url + ':' + line + ')'));};+ uncaughtExceptionHandlers.push(fn);}};@@ -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.diff --git a/test/browser/large.js b/test/browser/large.jsindex 7974bb6f..1250abae 100644--- a/test/browser/large.js+++ b/test/browser/large.js@@ -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);+ })})\ No newline at end of file