Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
fix report in a case of afterEach conditional fail
Browse files Browse the repository at this point in the history
Signed-off-by: Outsider <[email protected]>
  • Loading branch information
outsideris authored and boneskull committed Aug 10, 2018
1 parent 2bb4124 commit 02294a3
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
14 changes: 11 additions & 3 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,17 @@ Runner.prototype.runTests = function(suite, fn) {
}

test.state = 'passed';
self.emit('pass', test);
self.emit('test end', test);
self.hookUp('afterEach', next);

// For supporting conditional fail in afterEach hook,
// run emit pass after an afterEach hook.
// See, https://github.com/mochajs/mocha/wiki/HOW-TO:-Conditionally-fail-a-test-after-completion
self.hookUp('afterEach', function(err, errSuite) {
if (test.state === 'passed') {
self.emit('pass', test);
}
self.emit('test end', test);
next(err, errSuite);
});
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

describe('something', function() {
it('should one', function() {
this.ok = true;
});

it('should two', function() {
this.ok = false;
});

it('should three', function() {
this.ok = true;
});

afterEach(function() {
if (!this.ok) {
this.test.error(new Error('something went wrong'));
}
});
});
17 changes: 7 additions & 10 deletions test/integration/fixtures/multiple-done.fixture.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict';

// The suite below should result in an additional error, but does
// not. Uncomment once this bug is resolved.

// describe('suite', function() {
// beforeEach(function(done) {
// done();
// done();
// });
describe('suite', function() {
beforeEach(function(done) {
done();
done();
});

// it('test', function() {});
// });
it('test', function() {});
});

it('should fail in a test-case', function (done) {
process.nextTick(function () {
Expand Down
5 changes: 3 additions & 2 deletions test/integration/hook-err.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('hook error handling', function() {
describe('after each hook error', function() {
before(run('hooks/afterEach-hook-error.fixture.js'));
it('should verify results', function() {
assert.deepEqual(lines, ['test 1', 'after', bang + 'test 3']);
assert.deepEqual(lines, ['test 1', 'after', bang, 'test 3']);
});
});

Expand All @@ -62,7 +62,8 @@ describe('hook error handling', function() {
'1-2 before each',
'1-2 test 1',
'1-2 after each',
bang + '1 after each',
bang,
'1 after each',
'root after each',
'1-2 after',
'1 after',
Expand Down
20 changes: 18 additions & 2 deletions test/integration/hooks.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var assert = require('assert');
var runMocha = require('./helpers').runMocha;
var helpers = require('./helpers');
var splitRegExp = require('./helpers').splitRegExp;
var args = ['--reporter', 'dot'];

describe('hooks', function() {
it('are ran in correct order', function(done) {
runMocha('cascade.fixture.js', args, function(err, res) {
helpers.runMocha('cascade.fixture.js', args, function(err, res) {
var lines, expected;

if (err) {
Expand Down Expand Up @@ -49,4 +49,20 @@ describe('hooks', function() {
done();
});
});

it('can fail a test in an afterEach hook', function(done) {
helpers.runMochaJSON(
'hooks/afterEach-hook-conditionally-fail.fixture.js',
args,
function(err, res) {
if (err) {
done(err);
return;
}
assert.equal(res.stats.passes, 2);
assert.equal(res.stats.failures, 1);
done();
}
);
});
});
4 changes: 2 additions & 2 deletions test/integration/multiple-done.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('multiple calls to done()', function() {
it('results in failures', function() {
assert.equal(res.stats.pending, 0, 'wrong "pending" count');
assert.equal(res.stats.passes, 1, 'wrong "passes" count');
assert.equal(res.stats.failures, 1, 'wrong "failures" count');
assert.equal(res.stats.failures, 2, 'wrong "failures" count');
});

it('throws a descriptive error', function() {
Expand All @@ -35,7 +35,7 @@ describe('multiple calls to done()', function() {

it('results in failures', function() {
assert.equal(res.stats.pending, 0, 'wrong "pending" count');
assert.equal(res.stats.passes, 1, 'wrong "passes" count');
assert.equal(res.stats.passes, 0, 'wrong "passes" count');
assert.equal(res.stats.failures, 1, 'wrong "failures" count');
});

Expand Down
6 changes: 1 addition & 5 deletions test/integration/uncaught.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ describe('uncaught exceptions', function() {
return;
}
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.failures[0].title,
'should bail if a successful test asynchronously fails'
);
assert.equal(
res.passes[0].title,
'should bail if a successful test asynchronously fails'
);

assert.equal(res.code, 1);
done();
Expand Down

0 comments on commit 02294a3

Please sign in to comment.