3

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)
sridattas
  • 459
  • 1
  • 6
  • 21

1 Answers1

0

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

siuga
  • 31
  • 1
  • 10
  • THank you. I tried this and now I get the count of data properly but i also see promise is being rejected. I put a catch after then to see whats happening. Is call not going to testrunner? – sridattas Aug 27 '20 at 04:55
  • I verified and I see no issue with data returned from excel, its completing fine as I see the data.length being returned. I also verified, removing async from testrunner function then it worked but my requirement is to use async function for testrunner. Same behavior with promise.all as well. – sridattas Aug 27 '20 at 10:27
  • sorry, ignore my above comments. if i print testarray, it is printing all the data as array. So I feel no issue with excel. If I change spec-function to normal function then control goes to it and executes but its not same case if spec is inside a function. – sridattas Aug 27 '20 at 10:43
  • I think your problem about value dependant on other function result has already been solve. You are now suffering from another problem. Your error log is telling you that, you should only use the sub-function 'it' inside your 'describe' function. I believe it's not about a problem of promise, but usage of jasmine... – siuga Sep 05 '20 at 07:44
  • @sigua OK, Thank you. I will look into it. – sridattas Sep 14 '20 at 04:33