Skip to content

Commit

Permalink
Merge pull request #1578 from danielstjules/double-failures
Browse files Browse the repository at this point in the history
Ignore asynchronous failures after spec resolution
  • Loading branch information
Travis Jeffery committed Mar 6, 2015
2 parents 1830e01 + 210803c commit 49d81aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ test-outputs/issue1327/case-out.json: test/regression/issue1327/case.js
test-failing:
./bin/mocha \
--reporter $(REPORTER) \
test/acceptance/failing/timeout.js > /dev/null 2>&1 ; \
test/acceptance/failing > /dev/null 2>&1 ; \
failures="$$?" ; \
if [ "$$failures" != '2' ] ; then \
if [ "$$failures" != '4' ] ; then \
echo 'test-failing:' ; \
echo " expected 2 failing tests but saw $$failures" ; \
echo " expected 4 failing tests but saw $$failures" ; \
exit 1 ; \
fi

Expand Down
4 changes: 4 additions & 0 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ Runnable.prototype.run = function(fn){
var ms = self.timeout();
if (self.timedOut) return;
if (finished) return multiple(err || self._trace);

// Discard the resolution if this test has already failed asynchronously
if (self.state) return;

self.clearTimeout();
self.duration = new Date - start;
finished = true;
Expand Down
7 changes: 3 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,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) {
Expand Down
26 changes: 26 additions & 0 deletions test/acceptance/failing/uncaught-and-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

/**
* 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 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);
});

0 comments on commit 49d81aa

Please sign in to comment.