Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

fix to exit correctly when using bail flag

Outsider99c2f8aa7bf4
diff --git a/lib/runner.js b/lib/runner.js
index 565d47d6..3f4ba55e 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -249,6 +249,9 @@ Runner.prototype.fail = function(test, err) {
}
this.emit('fail', test, err);
+ if (this.suite.bail()) {
+ this.emit('end');
+ }
};
/**
@@ -278,9 +281,6 @@ Runner.prototype.failHook = function(hook, err) {
hook.originalTitle + ' for "' + hook.ctx.currentTest.title + '"';
}
- if (this.suite.bail()) {
- this.emit('end');
- }
this.fail(hook, err);
};
diff --git a/test/integration/fixtures/options/bail-with-before.fixture.js b/test/integration/fixtures/options/bail-with-before.fixture.js
new file mode 100644
index 00000000..dfc0f8d9
--- /dev/null
+++ b/test/integration/fixtures/options/bail-with-before.fixture.js
@@ -0,0 +1,11 @@
+'use strict';
+
+describe('suite1', function () {
+ before(function () {
+ throw new Error('this hook should be only displayed');
+ });
+
+ it('should not display this error', function () {
+ throw new Error('this should not be displayed');
+ });
+});
diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js
index a8119db6..3bb8ce88 100644
--- a/test/integration/options.spec.js
+++ b/test/integration/options.spec.js
@@ -69,6 +69,22 @@ describe('options', function() {
});
});
+ it('should stop all tests after the first error in before hook', function(done) {
+ run('options/bail-with-before.fixture.js', args, function(err, res) {
+ if (err) {
+ done(err);
+ return;
+ }
+ assert.equal(res.stats.pending, 0);
+ assert.equal(res.stats.passes, 0);
+ assert.equal(res.stats.failures, 1);
+
+ assert.equal(res.failures[0].title, '"before all" hook');
+ assert.equal(res.code, 1);
+ done();
+ });
+ });
+
it('should stop all hooks after the first error', function(done) {
run('options/bail-with-after.fixture.js', args, function(err, res) {
if (err) {