Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse beforeEach/afterEach in Jasmine JS?

When writing tests with JasmineJS I have many tests that have similar beforeEach/afterEach code.

Is there a way to implement an inheritance model using JasmineJS test suites?

I can group all tests in a single describe but in this case I will end with a single HUGE JS file containing all tests.

I would like to split the tests for each page.

Here is an example:

describe('Services Page', function() {      beforeEach(function() {         login_as_admin()     })      beforeEach(function() {         browser().navigateTo('/services')     })      if('Some test for services page', function() {})      afterEach(function() {         logout()     })  })   describe('Administrators Page', function() {      beforeEach(function() {         login_as_admin()     })      beforeEach(function() {         browser().navigateTo('/administrators')     })      if('Some test for administrators page', function() {})      afterEach(function() {         logout()     })  }) 
like image 918
Adi Roiban Avatar asked Jun 26 '13 10:06

Adi Roiban


People also ask

What is beforeEach in Jasmine?

For initializing and cleaning your specs, Jasmine provides two global functions, beforeEach() and afterEach() : The beforeEach function is called once before each spec in the suite where it is called. The afterEach function is called once after each spec in the suite where it's called.

What is the difference between beforeAll and beforeEach?

beforeEach(fn) Here the beforeEach ensures that the database is reset for each test. If beforeEach is inside a describe block, it runs for each test in the describe block. If you only need to run some setup code once, before any tests run, use beforeAll instead.

What is afterAll in Jasmine?

afterAll(functionopt, timeoutopt)Run some shared teardown once after all of the specs in the describe are run. Note: Be careful, sharing the teardown from a afterAll makes it easy to accidentally leak state between your specs so that they erroneously pass or fail.

When to use done Jasmine?

The done() call is made within the success() callback function to instruct jasmine that beforeEach() has terminated and it is now safe to continue with the it() function. There are no asynchronous events in the it() function, so the done() function is not utilized, although we could include it if we needed to.


1 Answers

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() {}); }); 
like image 125
Leo Gallucci Avatar answered Sep 19 '22 15:09

Leo Gallucci