Aneesh
Aneesh

Reputation: 599

Jasmine avoid beforeEach for certain tests

Is there a way to NOT execute beforeEach function only for certain tests ('it' blocks). Lets say I have 10 it blocks, I do not want beforeEach to be executed for two of the blocks. Is it possible?

Upvotes: 16

Views: 14822

Answers (2)

Aneesh
Aneesh

Reputation: 599

I currently managed this with a work around as follows:

var executeBeforeEach = true;
function beforeEach() {
    if(!executeBeforeEach) return;
    //your before each code here.
}
describe('some test case 1', function(){
  it('Start', function(){
    //this is a dummy block to disable beforeeach for next test
  })
  it('The test that does not need beforeEach', function(){
    //this test does not need before each.
  })
  it('Start', function(){
    //this is a dummy block to enable beforeeach for next test
  })

})

But, I am wondering if there is a more elegant way!?!

Upvotes: 3

Michael Radionov
Michael Radionov

Reputation: 13309

You can group specs which you want to run with beforeEach into a separate describe:

it('should 1...', function () {});
it('should 2...', function () {});

describe('group', function () {

    beforeEach(function () {
        // ...
    });

    it('should 3...', function () {});
    it('should 4...', function () {});

    // ...
});

Upvotes: 22

Related Questions