Skip to content

Commit

Permalink
Runner.prototype.uncaught: don't double-end runnables that already ha…
Browse files Browse the repository at this point in the history
…ve a state.

Fixes #1327
Closes #1335

Signed-off-by: Joshua Appelman <[email protected]>
  • Loading branch information
domq authored and Joshua Appelman committed Sep 9, 2014
1 parent 4f13ff5 commit 9c54aa2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ coverage.html
lib-cov
.DS_Store
node_modules
test-outputs
*.sock
testing
_mocha.js
Expand Down
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mocha.js: $(SRC) $(SUPPORT) lib/browser/diff.js lib/browser/escape-string-regexp

clean:
rm -f mocha.js
rm -rf test-outputs
rm -fr lib-cov
rm -f coverage.html

Expand All @@ -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-regression

test-jsapi:
@node test/jsapi
Expand All @@ -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="$$?" ; \
Expand Down
11 changes: 8 additions & 3 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 test
if ('test' == runnable.type) {
this.emit('test end', runnable);
Expand Down
11 changes: 8 additions & 3 deletions mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 test
if ('test' == runnable.type) {
this.emit('test end', runnable);
Expand Down
14 changes: 14 additions & 0 deletions test/regression/issue1327/case.js
Original file line number Diff line number Diff line change
@@ -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");
});
10 changes: 10 additions & 0 deletions test/regression/issue1327/control.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 9c54aa2

Please sign in to comment.