Search code examples
tddmocha.js

Conditional execution of mocha test cases


I am using Mocha with Sinon JS and Phantom Js to test the google analytics call from a particular page. Till now, i am able to execute static test cases for individual element by writing different test case for each element. Like :

describe("Site Home Page Test", function() {

    it ("Global Search track", function() {
        var link = $('button.search');
        link.click();
    });

});

Now the ask is, is it possible to execute test case if only $('elem') is found? something like this:

describe("Site Home Page Test", function() {

  //  if(condition) {

        it ("Global Search track", function() {
            var link = $('button.search');
            link.click();
        });

  //  }

});

Solution

  • I'm not sure if I've missed the question completly, but you can do conditional test cases exactly how you have it written:

    describe("Some module", function() {
        if(false) {
            it ("should NOT run this test case", function() { });
        }
    
        it("should run this test case", function() { });
    });
    

    will only run the that isn't in the if-statement.

    Some module
      ✓ should run this test case 
    
    1 passing (5 ms)