Maz
Maz

Reputation: 21

Can I have 2 before inside 1 describe using Mocha?

I have a describe for all test cases of one app page. I have a context that includes all positive test cases and then a context inside it to include all negative test cases. Before all test cases I have one before that includes the login. I want to know that I can add another before for the negative test cases.

Example:

describe('X page', function (){
  context('As a user', function (){
    before(function(){
      login goes here
    });
    it('Test case 1', function (){
      test case implementation goes here
    });
    it('Test case 2', function (){
      test case implementation goes here
    });
    context('Negative tests', function (){
      before(function(){
        negative tests precondition goes here
      });
      it('Test case 1', function (){
        test case implementation goes here
      });
      it('Test case 2', function (){
        test case implementation goes here
      });
    });
  });
});

Can that second before go there?

Upvotes: 2

Views: 1279

Answers (1)

Louis
Louis

Reputation: 151401

Yes, you can. The before hooks in outer describe are executed before those in the inner describe. And if you have multiple before hooks in the same callback to describe, they are executed in the order they appear. (Note that describe is a synonym for context: Mocha assigns the same function to both.)

Upvotes: 3

Related Questions