Yashraj basan
Yashraj basan

Reputation: 97

Cucumberjs - calling async methods in step definitions

I am calling api of jira in step definitions but it not working. i am using npm package request to make request call. ex -

function getIssue(){

request({url:"....."},function(err,response,body){

console.log(body)

})

}

step_definition

when("......",async function(){
         await getIssue();
   })

I found it is not going even in request callback.

Upvotes: 1

Views: 3003

Answers (3)

OhadR
OhadR

Reputation: 8839

As far as I understand you want to use async/await in the step definitions.

well, this one works for me:

const fsPromises = require("fs").promises;

Then(/...read file for example.../, async function (fileName) {
    const contents = await fsPromises.readFile(fileName, 'utf8');
    assert.equal(contents, 'expected content of file');
});

Upvotes: 1

user12841068
user12841068

Reputation: 1

try with this:

const { Given, When, Then } = require('@cucumber/cucumber');

const getIssue = async () => 
{
   //get the issue
}

when("......", function(){
   return getIssue();
})

From

Return a promise. The step is done when the promise resolves or rejects

Upvotes: 0

Sreenivasulu
Sreenivasulu

Reputation: 514

If you want to call any method using await (for ex: function getIssue()), that method/ function should be async first i.e. declare getIssue() as a async function and call it using await. If you are not declared it as async, call may go to the getIssue(), but by the time you get response, your scripts will move to the next line. Hence, if you need to wait till you get response from the function, make both getIssue() as async and call it by using await.

Upvotes: 0

Related Questions