Skip to content

armgit / mocha

Public Imported from GitHub
0

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

fix to bail option works properly with hooks (#3278)

JeongHoon Byun (aka Outsider)638391684a5a
diff --git a/lib/runner.js b/lib/runner.js
index 5a7e3e03..bc4d6523 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -265,10 +265,10 @@ Runner.prototype.failHook = function (hook, err) {
hook.title = hook.originalTitle + ' for "' + hook.ctx.currentTest.title + '"';
}
- this.fail(hook, err);
if (this.suite.bail()) {
this.emit('end');
}
+ this.fail(hook, err);
};
/**
diff --git a/test/integration/fixtures/options/bail-with-after.fixture.js b/test/integration/fixtures/options/bail-with-after.fixture.js
new file mode 100644
index 00000000..f7220929
--- /dev/null
+++ b/test/integration/fixtures/options/bail-with-after.fixture.js
@@ -0,0 +1,11 @@
+'use strict';
+
+describe('suite1', function () {
+ it('should only display this error', function () {
+ throw new Error('this should be displayed');
+ });
+
+ after(function () {
+ throw new Error('this hook should not be displayed');
+ });
+});
diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js
index 6c7390c0..a0105728 100644
--- a/test/integration/options.spec.js
+++ b/test/integration/options.spec.js
@@ -68,6 +68,22 @@ describe('options', function () {
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) {
+ 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, 'should only display this error');
+ assert.equal(res.code, 1);
+ done();
+ });
+ });
});
describe('--sort', function () {