0

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")
  });

});

1 Answer 1

1

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!");
}
5
  • Thank you! I realized I missed an extra condition, so I have edited the code. There is a setCheckCondition check and if that is true, then checkCondition would be set as true only if setCheckCondition is true first. I can't place the if before the 'it' block or else the execution order would be incorrect. Do you have any other ideas, since I can't replace beforeAll with the if check? Thanks
    – Suzy
    Commented Oct 1, 2021 at 5:11
  • If checkCondition is false you want to skip all the tests. So that means you don't even want to run the it blocks at all. Am I on the right track?
    – Steeve B
    Commented Oct 1, 2021 at 9:04
  • yes checkCondition can be either true or false depending on the result of setCheckCondition. If checkCondition is false, then skip all tests. If checkCondition is true, then run all tests
    – Suzy
    Commented Oct 1, 2021 at 16:49
  • Ok I'll update my answer
    – Steeve B
    Commented Oct 1, 2021 at 21:13
  • You still want the tests registered with the test runner, otherwise you have a variable number of tests. The devs may say, "hey, all the tests passed! Ship it!" without noticing that several hundred tests are missing. Consider e.g. const suite = checkCondition ? describe : describe.skip.bind(describe); and then using suite("text for describe", function() {...});
    – tehhowch
    Commented Nov 22, 2023 at 12:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.