☕️ Classic, reliable, trusted test framework for Node.js and the browser
Runner.prototype.uncaught: don't double-end runnables that already have a state.
Dominique Quatravaux9c54aa273df6
diff --git a/.gitignore b/.gitignoreindex 8df0ed78..011a79c7 100644--- a/.gitignore+++ b/.gitignore@@ -2,6 +2,7 @@ coverage.htmllib-cov.DS_Storenode_modules+test-outputs*.socktesting_mocha.jsdiff --git a/Makefile b/Makefileindex 4e60a768..72c4814c 100644--- a/Makefile+++ b/Makefile@@ -23,6 +23,7 @@ mocha.js: $(SRC) $(SUPPORT) lib/browser/diff.js lib/browser/escape-string-regexpclean:rm -f mocha.js+ rm -rf test-outputsrm -fr lib-covrm -f coverage.html@@ -35,7 +36,7 @@ lib-cov:test: test-unit-test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-sort test-glob test-requires test-reporters test-only test-failing+test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-sort test-glob test-requires test-reporters test-only test-failing test-regressiontest-jsapi:@node test/jsapi@@ -47,8 +48,17 @@ test-unit:--growl \test/*.js-test-failing:+test-regression: test-outputs/issue1327/case-out.json@./bin/mocha \+ --reporter $(REPORTER) \+ test/regression/issue*/control.js++test-outputs/issue1327/case-out.json: test/regression/issue1327/case.js+ @mkdir -p $(dir $@) || true+ @./bin/mocha --reporter json $< > $@ || true++test-failing:+ ./bin/mocha \--reporter $(REPORTER) \test/acceptance/failing/timeout.js > /dev/null 2>&1 ; \failures="$$?" ; \diff --git a/lib/runner.js b/lib/runner.jsindex 31196626..546f7a86 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -540,13 +540,18 @@ Runner.prototype.uncaught = function(err){debug('uncaught undefined exception');err = new Error('Catched undefined error, did you throw without specifying what?');}+ err.uncaught = true;var runnable = this.currentRunnable;- if (!runnable || 'failed' == runnable.state) return;- runnable.clearTimeout();- err.uncaught = true;+ if (!runnable) return;++ var wasAlreadyDone = runnable.state;this.fail(runnable, err);+ runnable.clearTimeout();++ if (wasAlreadyDone) return;+// recover from testif ('test' == runnable.type) {this.emit('test end', runnable);diff --git a/mocha.js b/mocha.jsindex a6fa2003..28b2fc62 100644--- a/mocha.js+++ b/mocha.js@@ -4993,13 +4993,18 @@ Runner.prototype.uncaught = function(err){debug('uncaught undefined exception');err = new Error('Catched undefined error, did you throw without specifying what?');}+ err.uncaught = true;var runnable = this.currentRunnable;- if (!runnable || 'failed' == runnable.state) return;- runnable.clearTimeout();- err.uncaught = true;+ if (!runnable) return;++ var wasAlreadyDone = runnable.state;this.fail(runnable, err);+ runnable.clearTimeout();++ if (wasAlreadyDone) return;+// recover from testif ('test' == runnable.type) {this.emit('test end', runnable);diff --git a/test/regression/issue1327/case.js b/test/regression/issue1327/case.jsnew file mode 100644index 00000000..295ec124--- /dev/null+++ b/test/regression/issue1327/case.js@@ -0,0 +1,14 @@+var debug = require('debug')('mocha:issue1327');+it("test 1", function() {+ debug("This runs only once.");+ process.nextTick(function() {+ throw "Too bad";+ });+});+it("test 2", function() {+ debug("This should run once - Previously wasn't called at all.");+});+it("test 3", function() {+ debug("This used to run twice.");+ throw new Error("OUCH");+});diff --git a/test/regression/issue1327/control.js b/test/regression/issue1327/control.jsnew file mode 100644index 00000000..b77555da--- /dev/null+++ b/test/regression/issue1327/control.js@@ -0,0 +1,10 @@+var assert = require("assert"),+ fs = require("fs");++describe("GitHub issue #1327: expected behavior of case.js", function() {+ it("should have run 3 tests", function() {+ var results = JSON.parse(fs.readFileSync(+ "test-outputs/issue1327/case-out.json"));+ results.stats.tests.should.equal(3);+ });+});