Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine avoid beforeEach for certain tests

Tags:

jasmine

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?

like image 470
Aneesh Avatar asked Jul 02 '15 10:07

Aneesh


2 Answers

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

    // ...
});
like image 130
Michael Radionov Avatar answered Sep 23 '22 18:09

Michael Radionov


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!?!

like image 40
Aneesh Avatar answered Sep 22 '22 18:09

Aneesh