Skip to content

Commit

Permalink
(re-)enable after each hooks to fail tests.
Browse files Browse the repository at this point in the history
In version 1.2.1, support was added to fail test in the after each hook:
>* Added `this.test.error(err)` support to after each hooks. Closes mochajs#287

While migrating from teaspoon-mocha to karma-mocha, we discovered that
in the current mocha version this isn't supported anymore. That
behaviour has been changed in: ebc6aee
without any documentation change.

If this was a conscious decision adding some documentation about
dropping this functionality would then be nice. The old behaviour is
documented in:
https://github.com/mochajs/mocha/wiki/Conditionally-failing-tests-in-afterEach()-hooks
.
  • Loading branch information
jjoos committed Mar 17, 2016
1 parent 9ae6a85 commit 80fc890
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
31 changes: 17 additions & 14 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,30 +532,33 @@ Runner.prototype.runTests = function(suite, fn) {
if (err instanceof Pending) {
test.pending = true;
self.emit('pending', test);
} else if (retry < test.retries()) {
return next();
}

if (retry < test.retries()) {
var clonedTest = test.clone();
clonedTest.currentRetry(retry + 1);
tests.unshift(clonedTest);

// Early return + hook trigger so that it doesn't
// increment the count wrong
return self.hookUp('afterEach', next);
} else {
self.fail(test, err);
}
self.emit('test end', test);

if (err instanceof Pending) {
return next();
}

return self.hookUp('afterEach', next);
self.fail(test, err);
} else {
test.state = 'passed';
}

test.state = 'passed';
self.emit('pass', test);
self.emit('test end', test);
self.hookUp('afterEach', next);
// Run the actual emit that the test passed after the forEach hooks,
// so they can still fail a test. And it's not already reported as
// passed.
self.hookUp('afterEach', function(err, errSuite) {
if (test.state === 'passed') {
self.emit('pass', test);
}
self.emit('test end', test);
next(err, errSuite);
});
});
});
}
Expand Down
16 changes: 16 additions & 0 deletions test/integration/fixtures/hooks/afterEach.hook.failure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe('spec 1', function () {
afterEach(function () {
this.test.error(new Error('something went wrong'));
});
it('should be set to failure in the afterEach', function () {
console.log('test 1');
});
it('should be set to failure in the afterEach', function () {
console.log('test 2');
});
});
describe('spec 2', function () {
it('should succeed', function () {
console.log('test 3');
});
});
17 changes: 17 additions & 0 deletions test/integration/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ describe('hooks', function() {
done();
});
});

describe('afterEach sets tests to fail', function() {
var res;
before(function(done) {
run('hooks/afterEach.hook.failure.js', args, function(err, result) {
res = result;
done(err);
});
});

it('afterEach can set a test to fail', function() {
assert.equal(res.pending, 0);
assert.equal(res.passing, 1);
assert.equal(res.failing, 2);
assert.equal(res.code, 2);
});
});
});
2 changes: 1 addition & 1 deletion test/integration/multiple.done.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('multiple calls to done()', function() {

it('results in failures', function() {
assert.equal(res.stats.pending, 0);
assert.equal(res.stats.passes, 1);
assert.equal(res.stats.passes, 0);
assert.equal(res.stats.failures, 1);
assert.equal(res.code, 1);
});
Expand Down

0 comments on commit 80fc890

Please sign in to comment.