6

Can I create a nested it in Protractor/Jasmine.

it("outer it", function () {
    it("inner it", function () {
        expect(1).toBe(1);
    });
});

I am trying to execute it test cases inside a loop, and in every iteration I want to run a test, for example:

it("outer it", function () {
    for(var i=0;i<10;i++){
        it("inner it", function () {
            expect(1).toBe(1);
        });
    }
});

The reason I want to do it is that I want to initialize an array and then in a dynamically way to loop trough all the element and run a number of "it", for example:

describe ("[Components]", function() {
   var grid = new Grid();

   it("Initialize the grid for the history window", function () {
       grid.init();
   });

   for(var i=0;i<grid.length;i++){
       it("test 1", function () {
           expect(1).toBe(1);
       });
   }

});

The grid.length is equal to 0 when the for loop execute, I want the for loop execute after the initialize "it".

user1684140
  • 444
  • 6
  • 18

2 Answers2

6

Answering to your question, no you cannot nest it's one inside other. Though Jasmine framework doesn't throw any error, the code inside a nested it doesn't execute. Also, I don't see any use of nesting it's as they are specs or functions that run on their own to complete a particular test step. It also gives an overview of the function that is being executed currently. If you are trying to run something in a loop, you can create another function and then call it inside the for loop, something like this -

it("outer it", function () {
    var newFunction = function(){
        expect(1).toBe(1);
    };
    for(var i=0;i<10;i++){
        newFunction();
    };
});

Hope this helps. More on it's can be found here - Jasmine Framework - it's

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • Hi Girish, I want to initialize an object using the DOM, that initialize have to be inside it ( tries to put it outside it and I get an error ), after that I want to run trough all the elementד inside the object I created, and for every element I need to run number of tests, I can use "expect", but I prefer to use "it" for more information when one of the test will fail. so the problem is that all the "it" goes to a queue, and all the code that not in "it" execute before, so if I have for that stop in a value that init in "it" the length in 0. – user1684140 Aug 05 '15 at 12:48
  • Hi user1684140, Try initializing the object inside the newFunction. It should work as shown above. Basically speaking **it**'s are used just to show the step being executed and not for looping, though you can loop inside an **it**. **it**'s always execute serially i.e., initialization **it** executes before your test1 **it**. If you want **for** loop to execute after initialization, use **then** chaining function available in protractor. – giri-sh Aug 05 '15 at 13:52
3
  1. As mentioned earlier - no, you can not place it inside of another it block, BUT you could place a whole describe block inside another one
  2. You also have an ability to run it blocks inside of for loops, OR for example make it block conditional.

You can find a real example of a code below (I've added for loop just for demonstration purposes)

describe("E2E: Environment configuration test", function () {

    beforeAll(function () {
        logIn();
    });

    afterAll(function () {
        logOut();
    });

    describe("Members page configuration test", function() {

    for (let i=0; i<3; i++) {
        it("Members page - columns", function () {
            //code that verifies that all columns of the page are presented
        });
    }

        it( "Members page - filters", function() {            
            //code that verifies that all filters of the UI are presented as expected
        });

        it( "Members page - eligibility feature", function() {            
            //code that verifies that if the feature is set to true then additional column must be displayed
        });

    });

    describe("Providers page configuration test", function() {

    // use of conditional it blocks
    // if feature is turned on run one set it blocks
    // otherwise execute another set of it blocks
        if (envConfig.providerFeature) {

            it( "Organizations tab configuration test", function() {            
                //code that verifies that all elements of the current tab are displayed according to configurations of all features of the application 
            });

            it( "Practitioners tab configuration test", function() {});
                //code that verifies that all elements of the current tab are displayed according to configurations of all features of the application 
        
        } else {

            it( "Providers page - verification of the page being disabled", function() {
                //code that verifies that both tabs are not present in the UI
                console.log('Providers feature is set to FALSE for the environment by default configuration, the test case will be skipped');
            });
        }

    });

    it( "Users page configuration test", function() {
         //code that verifies that all elements of the current page are displayed according to configurations of all features of the application 
    });

    it( "Reports page configuration test", function() {

        if (!envConfig.disabledReportsFeature) {
         //code that verifies that all elements of the current page are displayed according to configurations of all features of the application 
        } else {
            console.log('Reports DISABLED_REPORTS_FEATURE is set to TRUE for the environment by default configuration, the test case will be skipped');

        }
    });
});

The console output in this case will look well organized

enter image description here

P.S. Additionally I will attach an image of clean console

enter image description here

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • Note for others who are looking for async functionality, `describe` blocks do not allow for async functions (while `forAll/Each` & `it` blocks do). – wraiford Mar 12 '23 at 16:13