How to reuse beforeEach/afterEach in Jasmine JS?

I think this is partially examined in this blog post and also answered here but i’m adding an adapted answer for your example:

Reusable code:

function sharedSetup(startPage) {
    beforeEach(function() {
        login_as_admin();
        browser().navigateTo(startPage);
    });

    afterEach(function() {
        logout();
    });
};

How to use it:

describe('Services Page', function() {
    sharedSetup('/services');

    it('Some test for services page', function() {});
});

describe('Administrators Page', function() {
    sharedSetup('/administrators');

    it('Some test for administrators page', function() {});
});

Leave a Comment