How to skip some specs if a certain test fails - WebdriverIO+Mocha(JavaScript)

Viewed 35

I want to run certain specs only if feature related with these specs works correctly and I want to check it in the "Before" hook (by using try/catch construction and the global variable). Something like this:

        before: async function (capabilities, specs) {
            try {
                await IndexPage.checkImportantFeature();
                process.env.RESULT = 'pass';
            } catch {
                console.log('failed test')
                process.env.RESULT = 'fail';
            }
          }

And in the specs, something like this:

if (process.env.RESULT == 'pass') {
  describe(`My Test Spec`, () => {
    it(`Test 1`, () => {
      // body
    });
  
    it(`Test 2`, () => {
      // body
    });
  });
} else {
  console.log("Feature check failed. Skipping all tests!");

But for me it doesn't work. I can use only "Before" hook because other similiar hooks ( beforeSession, onPrepare) won't let me run the test and with "before" hook I get a problem with with initializing of the specs. With a code above I get the error "Failed launching test session: TypeError: Cannot set property 'failures' of undefined" - so "before" hook doesn't even start. And if I set some default value to process.env.RESULT, the value of this variable doesn't change after the "before" hook, I mean it changes but specs don't see new value and use only default value.


So, I just want to run a simple UI test before all specs and if this test fails, skip some of the specs in test run (with using a global variable).

0 Answers
Related