Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Ignore async global errors after spec resolution

Daniel St. Jules210803c12c45
diff --git a/Makefile b/Makefile
index 451ba330..9e6e8318 100644
--- a/Makefile
+++ b/Makefile
@@ -62,9 +62,9 @@ test-failing:
--reporter $(REPORTER) \
test/acceptance/failing > /dev/null 2>&1 ; \
failures="$$?" ; \
- if [ "$$failures" != '3' ] ; then \
+ if [ "$$failures" != '4' ] ; then \
echo 'test-failing:' ; \
- echo " expected 3 failing tests but saw $$failures" ; \
+ echo " expected 4 failing tests but saw $$failures" ; \
exit 1 ; \
fi
diff --git a/lib/runner.js b/lib/runner.js
index 80246a12..dfa398f6 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -569,12 +569,11 @@ Runner.prototype.uncaught = function(err){
var runnable = this.currentRunnable;
if (!runnable) return;
- var wasAlreadyDone = runnable.state;
- this.fail(runnable, err);
-
runnable.clearTimeout();
- if (wasAlreadyDone) return;
+ // Ignore errors if complete
+ if (runnable.state) return;
+ this.fail(runnable, err);
// recover from test
if ('test' == runnable.type) {
diff --git a/test/acceptance/failing/uncaught-and-async.js b/test/acceptance/failing/uncaught-and-async.js
index 531e3dbe..054d1d85 100644
--- a/test/acceptance/failing/uncaught-and-async.js
+++ b/test/acceptance/failing/uncaught-and-async.js
@@ -1,15 +1,26 @@
'use strict';
/**
- * This file should only generate one test error despite the fact that Mocha is
- * capable of detecting two distinct exceptions during test execution.
+ * This file should only generate one failure per spec despite the fact that
+ * Mocha is capable of detecting two distinct exceptions during test execution.
*/
-it('fails exactly once', function(done) {
+it('fails exactly once when a global error is thrown first', function(done) {
setTimeout(function() {
+ throw new Error('global error');
+
setTimeout(function() {
done(new Error('test error'));
}, 0);
+ }, 0);
+});
+
+it('fails exactly once when a global error is thrown second', function(done) {
+ setTimeout(function() {
+ done(new Error('test error'));
+ }, 0);
+
+ setTimeout(function() {
throw new Error('global error');
}, 0);
});