Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix report in a case of afterEach conditional fail #3362

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this related?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.split(splitRegExp)

In here,splitRegExp is [\n.]+.
So, prev result is .!test 3, but now the result is !.test 3.
Therefore, ! and test 3 is separated.

});
});

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be easier to just look at the output of runMochaJSON()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to use it.

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