Skip to content

Commit ab6db92

Browse files
authored
Merge pull request #698 from hapijs/better-bail
Improve bail strategy not to execute next befores/afters
2 parents 5bfeb49 + c760bde commit ab6db92

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

lib/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ internals.executeExperiments = function (experiments, state, skip, callback) {
285285
const previousDomains = state.domains;
286286
state.domains = [];
287287

288-
const skipExperiment = skip || experiment.options.skip || !internals.experimentHasTests(experiment, state);
288+
const skipExperiment = skip || experiment.options.skip || !internals.experimentHasTests(experiment, state) || (state.options.bail && state.report.failures);
289289
const steps = [
290290
function (next) {
291291

test/runner.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,107 @@ describe('Runner', () => {
819819
});
820820
});
821821

822+
it('bail will terminate on the first test failure (skipping next befores)', (done) => {
823+
824+
const script = Lab.script();
825+
let beforeRan = false;
826+
script.experiment('root', () => {
827+
828+
script.experiment('test', () => {
829+
830+
script.test('1', (testDone) => {
831+
832+
testDone();
833+
});
834+
835+
script.test('2', (testDone) => {
836+
837+
throw new Error('bailing');
838+
});
839+
840+
script.test('3', (testDone) => {
841+
842+
testDone();
843+
});
844+
});
845+
846+
script.experiment('test', () => {
847+
848+
script.before((done) => {
849+
850+
beforeRan = true;
851+
done();
852+
});
853+
854+
script.test('1', (testDone) => {
855+
856+
testDone();
857+
});
858+
});
859+
});
860+
861+
Lab.execute(script, { bail: true }, null, (err, notebook) => {
862+
863+
expect(err).not.to.exist();
864+
expect(notebook.tests).to.have.length(4);
865+
expect(notebook.failures).to.equal(1);
866+
expect(notebook.tests[1].err.message).to.contain('bailing');
867+
expect(notebook.tests[2].skipped).to.be.true();
868+
expect(notebook.tests[3].skipped).to.be.true();
869+
expect(beforeRan).to.be.false();
870+
done();
871+
});
872+
});
873+
874+
it('bail will terminate on the first test failure (skipping sub-experiments)', (done) => {
875+
876+
const script = Lab.script();
877+
let beforeRan = false;
878+
script.experiment('test', () => {
879+
880+
script.test('1', (testDone) => {
881+
882+
testDone();
883+
});
884+
885+
script.test('2', (testDone) => {
886+
887+
throw new Error('bailing');
888+
});
889+
890+
script.test('3', (testDone) => {
891+
892+
testDone();
893+
});
894+
895+
script.experiment('test', () => {
896+
897+
script.before((done) => {
898+
899+
beforeRan = true;
900+
done();
901+
});
902+
903+
script.test('1', (testDone) => {
904+
905+
testDone();
906+
});
907+
});
908+
});
909+
910+
Lab.execute(script, { bail: true }, null, (err, notebook) => {
911+
912+
expect(err).not.to.exist();
913+
expect(notebook.tests).to.have.length(4);
914+
expect(notebook.failures).to.equal(1);
915+
expect(notebook.tests[1].err.message).to.contain('bailing');
916+
expect(notebook.tests[2].skipped).to.be.true();
917+
expect(notebook.tests[3].skipped).to.be.true();
918+
expect(beforeRan).to.be.false();
919+
done();
920+
});
921+
});
922+
822923
it('dry run won\'t execute tests', (done) => {
823924

824925
const script = Lab.script();

0 commit comments

Comments
 (0)