Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have a beforeAll function in Jasmine ? (Not coffeeScript)

I need to know if there is a way to include or use a beforeAll function, or something similar, so I can login to my application and then start testing.

Right now I'm putting my login operations in the first test case ( it ). Which is not a good practice.

If there is a better way to store my login code other then using a beforeAll function please tell me about it.

I'm using pure Jasmine not related to any other framework like coffee-script or others.

Thank you

like image 410
pharaon450 Avatar asked Jun 13 '14 15:06

pharaon450


People also ask

What is beforeAll in Jasmine?

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

What is the difference between beforeAll and BeforeEach?

Given the articles I found that BeforeAll is called once and only once. While the BeforeEach is called before each individual test.


2 Answers

This is now much easier. As of Jasmine 2.1 (released 14 Nov 2014), there is a beforeAll function built into the framework.

Here are the release notes with everything that was added in 2.1. And here is the documentation explaining beforeAll and afterAll

like image 97
andypaxo Avatar answered Sep 27 '22 17:09

andypaxo


You can nest as many describe functions as you want. So you can do something like...

describe("General Test", function () {      function login(){         //This code will run once at he beginning of your script     };      login();      beforeEach(function () {         //anything in here will apply to everything in each nested describe     });      describe("Specific Test", function () {         //Applied here     });      describe("Another Specific Test", function () {         //And here     });   }); 
like image 33
j_buckley Avatar answered Sep 27 '22 17:09

j_buckley