Be careful when using loops in Jasmine specs

Your test automation code is an important tool to evaluate your application.  You must ensure your test code is working/testing as you want it to be. the automated tests give confidence for the developer that the application behaves as expected and things are not broken after they change the code.

Recently, at our QA phase, the QA found a bug that should have been caught from our test automation. Therefore being suspicious I looked into the code.  Interesting enough, the way we used loops along with test blocks were not testing as we wanted. Below is an example of a test block with the bad approach we had. Then I listed down a working approach using loops.

describe('Bad Spec', function () {
 for (var i = 0; i < 3; i++) {
   console.log('Iteration value outside test: ' + i);
   it ('bar', function() {
     console.log('Test is evaluated. Iteration value inside test: ' + i);
     expect(true).toEqual(true)
   });
 }
});

The following is the console output. Notice that the value inside the test block is always the same value.

Iteration value outside test: 0
Iteration value outside test: 1
Iteration value outside test: 2
Test is evaluated. Iteration value inside test: 3
Test is evaluated. Iteration value inside test: 3
Test is evaluated. Iteration value inside test: 3

 

Here a solution is to use a function

describe('Good Spec', function () {
  function goodRunner(itemValue) {
    it ('foo', function() {
      console.log('Test is evaluated. Iteration value inside test: ' + itemValue);
      expect(true).toEqual(true)
    });
  }
  for (var i = 0; i < 3; i++) {
    console.log('Iteration value outside test: ' + i);
    goodRunner(i);
  }
});

Now the output looks as follows.

Iteration value outside test: 0
Iteration value outside test: 1
Iteration value outside test: 2
Test is evaluated. Iteration value inside test: 0
Test is evaluated. Iteration value inside test: 1
Test is evaluated. Iteration value inside test: 2

 

 

 

 

 

 

5 1 vote
Article Rating

Posted

in

by

Tags:

Comments

6 responses to “Be careful when using loops in Jasmine specs”

  1. Suren Avatar
    Suren

    For me it is not working.Please help

    1. Sinaru Gunawardena Avatar

      Please be more specific.

  2. Deepak Avatar
    Deepak

    Works well with above approach

  3. Deepa Avatar
    Deepa

    HI…
    I am new to protractor.
    I want to import excel values(from 6 rows) and to call same login function using for() loop. When i use , it() block inside function, function is called. but it() block doesnt executed.

  4. c. aiello Avatar
    c. aiello

    it should work fine if you use an arrow function “it (‘bar’, () => {…” instead of using “it (‘bar’, function() {…”

  5. Varun Avatar
    Varun

    Also, should work if we change the var to let in the for loop sue to block scoping.

Leave a Reply

Your email address will not be published. Required fields are marked *

6
0
Would love your thoughts, please comment.x
()
x