Mocha: How to bail only failed describe() in nested describe() ?

Created on 20 Jun 2017  路  6Comments  路  Source: mochajs/mocha

My test structure is as follows

describe('Test Suite'){
     describe('First Test Case'){
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
     describe('SecondTest Case'){
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
}

I want to use --bail such that if inside First Test Case any it() fails then that describe() should be bailed.But the Second Test Case should run.

I am getting the expected result by using:

describe('Test Suite'){
     this.bail(false)
     describe('First Test Case'){
        this.bail(true);
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
     describe('SecondTest Case'){
        this.bail(true);
        it('1st step'){};
        it('2nd step'){};
        it('3rd step'){};
    }
}

Is there any other way to do it without explicitly assigning bail to true in each describe()?

needs-feedback question stale

Most helpful comment

I found work-around, maybe it will help:

In setup file:

const FAILED_TESTS = {};
// Skip test if first test from folder failed
beforeEach(function() {
  if (FAILED_TESTS[this.currentTest.file]) {
    this.skip();
  }
});

afterEach(function() {
  if (this.currentTest.state === "failed") {
    FAILED_TESTS[this.currentTest.file] = true;
  }
});

All 6 comments

That would be awesome to have such option like --bail-describe. It's often does not make sense to continue current describe if any test case fails but still make sense to go to another one.

does this.bail() in beforeEach() or before() work?

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

I would vote to re-open it. It does not work in beforeEach / before

I found work-around, maybe it will help:

In setup file:

const FAILED_TESTS = {};
// Skip test if first test from folder failed
beforeEach(function() {
  if (FAILED_TESTS[this.currentTest.file]) {
    this.skip();
  }
});

afterEach(function() {
  if (this.currentTest.state === "failed") {
    FAILED_TESTS[this.currentTest.file] = true;
  }
});

If you only need to bail from the particular describe (and not the file per se), the following work-around also works (inspired by @artyomtrityak's work-around):

let failed = false;
// Skip test if any prior test in this describe failed
beforeEach(function() { failed && this.skip() });
afterEach(function() { failed = this.currentTest.state === "failed" });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SGD1953 picture SGD1953  路  75Comments

ghost picture ghost  路  32Comments

Almad picture Almad  路  41Comments

domenic picture domenic  路  43Comments

teckays picture teckays  路  84Comments