Global `beforeEach` in jasmine?

被刻印的时光 ゝ 提交于 2019-12-31 10:57:11

问题


I'm using Jasmine to write tests.

I have several test files, each file has a beforeEach, but they are exactly the same.

How do I provide a global beforeEach for them?


回答1:


You can put it in your spec_helper.js file and it should work fine.




回答2:


x1a4's answer confused me. This may be more clear:

When you declare a beforeEach function outside all describe blocks, it will trigger before each test (so before each it). It does not matter if you declare the beforeEach before or after your describe blocks.

It's not mentioned in the documentation.

// Example: 

beforeEach(function() {
    localStorage.clear();
});

describe('My tests', function() {
    describe('Test localstorage', function() {

        it('Adds an item to localStorage', function() {
            localStorage.setItem('foo', 'bar');
            expect(localStorage.getItem('foo')).toBe('bar');
        });

        it('Is now empty because our beforeEach cleared localStorage', function() {
            expect(localStorage.getItem('foo')).toBe(null);
        });

    });
});


来源:https://stackoverflow.com/questions/10560716/global-beforeeach-in-jasmine

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