Skip to content Skip to sidebar Skip to footer

How To Skip To Next Next Describe On Error In Mocha?

I have a bunch of describes that test different parts of an API. In one section, all the tests are dependent on one test succeeding. I want to make Mocha run the first test, and if

Solution 1:

Put what you call your "first test" in a before hook inside a describe block that contains all the other tests:

describe("bunch of related tests", function () {
    before(function () {
        // "first test" here
    });

    it("test 1", function () { ... });
    it("test 2", function () { ... });
    // ...
});

This is the proper way in "vanilla Mocha" to set a dependency between the code in the before hook and each of the tests. If the before hook fails, Mocha will report it, and it will skip all the tests in the describe block. If you have other tests elsewhere, they will still run.

Solution 2:

Although I up-voted the accepted answer, I wasn't able to get a Mocha it test to run inside a before function. Instead I had to separate the first test into its own describe and set a variable if the test passed, then check the variable in the before of the describe containing all the other tests.

let passed = falsedescribe('first test', function() {
  it('run the first test', function(done) {
    if (theTestPassed)
      passed = truedone()
  })
})

describe('rest of the tests', function() {
  before(function() {
    if (!passed)
      thrownewError('skip rest of the tests')
  });
  it('second test', ...)
  it('third test', ...)
});

Post a Comment for "How To Skip To Next Next Describe On Error In Mocha?"