Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional execution of mocha test cases

Tags:

tdd

mocha.js

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

  //  }

});
like image 844
Lokesh Yadav Avatar asked Jul 16 '13 09:07

Lokesh Yadav


People also ask

Does Mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.

Is Mocha BDD or TDD?

With its default “BDD”-style interface, Mocha provides the hooks before() , after() , beforeEach() , and afterEach() . These should be used to set up preconditions and clean up after your tests.

How do you skip test cases in Mocha?

You can skip tests by placing an x in front of the describe or it block, or placing a . skip after it. describe('feature 1', function() {}); describe.


1 Answers

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() { });
});

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

Some module
  ✓ should run this test case 

1 passing (5 ms)
like image 137
Jay Avatar answered Oct 11 '22 20:10

Jay