28

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

pharaon450
  • 493
  • 3
  • 9
  • 21

4 Answers4

36

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

andypaxo
  • 6,171
  • 3
  • 38
  • 53
4

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


});
j_buckley
  • 1,926
  • 1
  • 12
  • 13
  • 1
    Does that means that the beforeEach is going to run before every nested Describe ? or before every it ? – pharaon450 Jun 13 '14 at 15:29
  • 1
    It will run before every `it` function within every nested `describe` – j_buckley Jun 13 '14 at 15:32
  • Then it's not what I need. I need something to make run some stuff _BEFORE ALL_ the `it` – pharaon450 Jun 13 '14 at 15:34
  • Can you post the login code that you want to run before all? There may be a way to incorporate it using this method. I have never seen a before all function the way you describe though. Also this answer to a related question might help...http://stackoverflow.com/a/24046016/3711699 – j_buckley Jun 13 '14 at 15:52
  • Do you want this to run multiple times or can it just run once at the beginning of the Jasmine script? Because if that is the case you can just write your code before you begin any describe functions. I'll edit my code to show you what I mean. – j_buckley Jun 13 '14 at 16:02
  • yes exactly I need the make run the login operation before all the `it`. I will try your way. Thanks – pharaon450 Jun 13 '14 at 17:30
  • So will that work for you? The way my answer is set up now you can put your code in the `login()` function where it will run once at the start of your script or you can put your code in the beforeEach and it will run before every `it`. Will either of those work for you? – j_buckley Jun 13 '14 at 17:38
  • 3
    Can someone explain why the BeforeAll function is even necessary? Seems redundant if you can just run the function prior (as seen in this answer) - and doesn't provide any extra sematic value either. – aaaaaa Dec 01 '14 at 09:12
  • 1
    @aaaaaa beforeAll() is necessary because in this example login() will run before all test cases in all spec files. But what you really need is to run login() before all the tests in an arbitrary describe block are run. Let's say you have another spec that requires the user to be logged out, so it calls logout(). All tests in both files will either be run logged in or logged out depending on which spec was loaded first. – Johnny C May 18 '16 at 14:55
  • @JCSG - Your comment doesn't jive with [jasmine's documentation](http://jasmine.github.io/2.1/introduction.html#section-10) which places beforeAll inside a describe block and implements the same behavior seen in j_buckley's answer. – aaaaaa May 18 '16 at 16:19
  • 4
    @aaaaaa It jibes with the documentation, actually. `The beforeAll function is called only once before all the specs in describe are run` whereas "naked" inside a describe block will run once before all specs in all spec files runs, not just in that describe block. For instance, if I wanted to use one mongo database for one describe block, and one for another describe block, I need to use beforeAll() or they will all use the same database. – Johnny C May 18 '16 at 16:26
  • Aye you're totally right. Very sorry. Just tested it now and I understand the flow better. Much appreciated. – aaaaaa May 18 '16 at 17:58
2

please use below code and configure your setting in beforeAll statement.

describe("Top", function() {
     beforeAll(function() { 
            console.log("Example 1 Setup"); 
     });
     it('xyz',function(){
        console.log('Hi!')
     });
});
jaibalaji
  • 3,159
  • 2
  • 15
  • 28
1

You can add this package that adds a beforeAll() and afterAll() to Jasmine.

https://github.com/nonplus/jasmine-beforeAll

Alexander Zanfir
  • 544
  • 5
  • 14