Skip to content

Commit 85a36fd

Browse files
committed
fix to exit correctly when using bail flag
1 parent efff26f commit 85a36fd

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/runner.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ Runner.prototype.fail = function (test, err) {
241241
}
242242

243243
this.emit('fail', test, err);
244+
if (this.suite.bail()) {
245+
this.emit('end');
246+
}
244247
};
245248

246249
/**
@@ -269,9 +272,6 @@ Runner.prototype.failHook = function (hook, err) {
269272
hook.title = hook.originalTitle + ' for "' + hook.ctx.currentTest.title + '"';
270273
}
271274

272-
if (this.suite.bail()) {
273-
this.emit('end');
274-
}
275275
this.fail(hook, err);
276276
};
277277

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
describe('suite1', function () {
4+
before(function () {
5+
throw new Error('this hook should be only displayed');
6+
});
7+
8+
it('should not display this error', function () {
9+
throw new Error('this should not be displayed');
10+
});
11+
});

test/integration/options.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ describe('options', function () {
6969
});
7070
});
7171

72+
it('should stop all tests after the first error in before hook', function (done) {
73+
run('options/bail-with-before.fixture.js', args, function (err, res) {
74+
if (err) {
75+
done(err);
76+
return;
77+
}
78+
assert.equal(res.stats.pending, 0);
79+
assert.equal(res.stats.passes, 0);
80+
assert.equal(res.stats.failures, 1);
81+
82+
assert.equal(res.failures[0].title, '"before all" hook');
83+
assert.equal(res.code, 1);
84+
done();
85+
});
86+
});
87+
7288
it('should stop all hooks after the first error', function (done) {
7389
run('options/bail-with-after.fixture.js', args, function (err, res) {
7490
if (err) {

0 commit comments

Comments
 (0)