I have a bunch of describe
s 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 it fails, skip all following tests and run the next test suite for the next section of the API.
mocha --bail
would stop testing altogether after the first fail, and won't continue to the next section.
mocha-steps
is a viable solution, but I prefer not to use any external libraries. In addition, it doesn't skip
steps after the failure, it doesn't print them altogether. Like I said, it's a viable solution, but not ideal.
What would be the best way to implement this behavior in vanilla Mocha?
Although I up-voted the accepted answer, I wasn't able to get a Mocha
it
test to run inside abefore
function. Instead I had to separate the first test into its owndescribe
and set a variable if the test passed, then check the variable in thebefore
of thedescribe
containing all the other tests.Put what you call your "first test" in a
before
hook inside adescribe
block that contains all the other tests: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 thebefore
hook fails, Mocha will report it, and it will skip all the tests in thedescribe
block. If you have other tests elsewhere, they will still run.