How to run a function based on variable value which is dependent on other function in specs jasmine?

Viewed 79

Requirement is to run testRunner function based on variable 'data' which is dependent on function 'getSteps'

My code as below

    function getSteps()
    it('test', async function () {
            testStepsArray =  await excel.getColValue('smoke.xlsx', 'Sheet1', 'A')
            return testStepsArray
        });
    }  
    
 function testRunner(count) { **//i want to run this function based on value of data variable below**
          it('test', async function () {        
                for(var j=0;j<count;j++)
                {
                    ...
                }
                            
            });
        }  
        
        
    var data =  getSteps(); 
    console.log(data.length+"LENGTH")    **//returns Cannot read property 'length' of undefined.**
        for(var i=1; i<= data.length;i+=count)   
        {
            ... 
            testRunner(i)
        }

i think last block is not waiting for results of getSteps. Please suggest.

Update: After inputs, I modified as below and i see difference. I now can get data values properly but execution fails when there is a function wrapped around spec but works with regular function

function testRunner(count) { 
 it('test', async function () {     
    for(var j=0;j<count;j++)
    {
            ...
    }               
   });
}  
    
function test(){
    ...
}  

let promise = new Promise((resolve,reject)=>{
    it('test', async function () {
            testStepsArray =  await excel.getColValue('smoke.xlsx', 'Sheet1', 'A')
            resolve(testStepsArray)
        });
    }  
})
promise.then((data)=>{
    console.log(data.length+"LENGTH")
        for(var i=1; i<= data.length;i+=count)   
        {
            testRunner(i) //fails if function is like testrunner() - function wrraped around specs
            test() //works if function is like test() - regular function
        }
})

Update 2:Promise rejection Error logs

[32m. [0mError: 'it' should only be used in 'describe' function
    at ensureIsNotNested (\nodejs\node_modules\jasmine_3.5.0\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1786:15)
    at Env.it (\nodejs\node_modules\jasmine_3.5.0\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1933:7)
    at it (nodejs\node_modules\jasmine_3.5.0\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:6576:21)
    at testRunner (webapps\mysamples\testRunner.js:66:4)
    at webapps\mysamples\testRunner.js:59:4
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
1 Answers

What about Promise? It waits until your function resolve "the data"

let promise = new Promise((resolve,reject)=>{
    it('test', async function () {
            testStepsArray =  await excel.getColValue('smoke.xlsx', 'Sheet1', 'A')
            resolve(testStepsArray)
        });
    }  
})
promise.then((data)=>{
    console.log(data.length+"LENGTH")
        for(var i=1; i<= data.length;i+=count)   
        {
            ... 
            testRunner(i)
        }
})

Read more about promise

Update

A rejected promise means your execution of code is not work as expected.

i.e. A website GET request.

let promise = new Promise((resolve,reject)=>{
    $.get(some_website, function(data, status, error){
        if(status==200){
            //Get request success, then return the data from website
            resolve(data);
        } else {
            //Get request fail, return error message
            reject(error);
        }
    });
})

I guess you are not successfully running excel.getColValue('smoke.xlsx', 'Sheet1', 'A') try to add the reject(something) for fail case. Then in your consuming part, add this:

promise.then((data)=>{
    //blah blah blah
}).catch((err)=>{
   console.log(err) //read what's up with your operation
})

Or if you find that your promise got stuck in the for loop(did not wait the for loop complete, please use promise.all() References

Related