Jasmine avoid beforeEach for certain tests

徘徊边缘 提交于 2021-02-18 10:45:29

问题


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?


回答1:


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

    // ...
});



回答2:


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



来源:https://stackoverflow.com/questions/31182170/jasmine-avoid-beforeeach-for-certain-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!