简体   繁体   中英

Mocha shuts down whole test suite on beforeEach error

I'm using TypeScript with Mocha and Selenium. My problem is that if an Error gets thrown in beforeEach the whole test suite gets shut down. I'll try to illustrate it, this is how it would run on successful tests:

TestSuite A
     beforeEach hook for Test A
     Test A
     beforeEach hook for Test B
     Test B
     ...

This is how it currently runs if an Error occurres in "beforeEach hook for Test A":

TestSuite A
     beforeEach hook for Test A -> Error gets thrown
     TestSuite A gets exited

This is a problem for me because I'm using a Selenium wait in my beforeEach hooks. I'm waiting for a text to be changed on my webpage to "connected". This indicates that my clients have established a successful connection which is what I want. I'm basically setting up my environment in my beforeEach hooks.

This is how I'd like for it to run, this seems the most natural to me but Mocha for some reason does not work this way (while basically every other framework does work this way, like RSpec, JUnit, XUnit etc.)

TestSuite A
     beforeEach hook for Test A -> Error gets thrown
     beforeEach hook for Test B
     Test B
     ...

I'm using Node version v14.15.4 and Mocha 8.2.1. Thanks in advance!

According to this issue on github it says:

mocha currently assumes that if you have an issue in before hooks then subsequent things will fail, though we could limit the scope of that assumption to only those test-cases nested within the same describe()

Also is a normal behaviour stop execution is an error has been thrown.

You can use a try/catch block to handle the exception or even if the error is a must, you can use assert.throw to check the error exists and has been thrown.

beforeEach('some description', function() {
  assert.throw(() => {your_function()})
});

So, the subsequent test will be executed if an error has been thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM