From b1ba3df9d9bd298f4e3a614e6817ee811cf0f415 Mon Sep 17 00:00:00 2001 From: Outsider Date: Sun, 22 Apr 2018 22:48:40 +0900 Subject: [PATCH] fix to exit correctly when using bail flag --- lib/runner.js | 6 +++--- .../fixtures/options/bail-with-before.fixture.js | 11 +++++++++++ test/integration/options.spec.js | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/integration/fixtures/options/bail-with-before.fixture.js diff --git a/lib/runner.js b/lib/runner.js index 565d47d6e5..3f4ba55ef0 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 0000000000..dfc0f8d939 --- /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 a8119db695..3bb8ce8823 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) {