Suzy
Suzy

Reputation: 261

Is there a way to skip all tests in a suite if a certain conditions fail - JavaScript Jasmine?

Is there a way to skip all tests in a suite if a certain conditions fail? I.e) if the webpage is not opening, there is no point in running the rest of the tests, because they all depend on the webpage opening before any tests are run.

We can use pending() to skip the current test, but is there a way to skip all the tests in the suite or tests below right away if all the other tests depend on checkCondition to be true?

I tried adding pending() inside the beforeAll block, to try to skip all the tests since beforeAll is run before anything.

Please help! I am using WebdriverIO and Jasmine. Thank you!

let checkCondition = false;

describe(`My Test Suite`, () => {
  beforeAll(async () => {
     // EDIT - returnBoolean() is another method that logs into the 
     // page and returns true or false
     let setCheckCondition = returnBoolean();

    if (setCheckCondition) {
      checkCondition = true;
      console.log(`in true block`);
    } else {
      console.log('Skip tests below');  // HELP <-- since checkCondition is false, all tests below should fail 
      pending();
    }
  });

  it(`Test 1`, () => {
    if (checkCondition != undefined) {
      console.log("checkCOndition is defined")
    } else {
      pending();
    }
  });

  it(`Test 2`, () => {
    if (checkCondition) {
      // check 
    } else {
      console.log('Skip this test');
      pending();
    }
  });

  it(`Test 3`, () => {
    console.log("skip this test too")
  });

});

Upvotes: 0

Views: 2458

Answers (1)

Steeve B
Steeve B

Reputation: 103

If you have to set checkCondition in beforeAll and then eventually abort from there, there seems to be an ongoing issue about it on github (https://github.com/jasmine/jasmine/issues/1533)

Otherwise if checkConditon can be known before the test suite begins (I don't see why not) you can just just replace your beforeAll with something like

if (!checkCondition) return

Or just skip the whole call to describe in the first place

EDIT: You can skip the whole test suite like that:

let checkCondition = returnBoolean();
if (checkCondition) {
  describe(`My Test Suite`, () => {
    it(`Test 1`, () => {
      // run test 1 normally
    });
  
    it(`Test 2`, () => {
      // run test 2 normally
    });
  
    it(`Test 3`, () => {
      // run test 3 normally
    });
  
  });
} else {
  console.log("checkCondition is false. Skipping all tests!");
}

Upvotes: 1

Related Questions