ljs
ljs

Reputation: 533

Nodejs test giving error when part of the test is modularized into a different function

I have a Nodejs test which has below part

const response1 = await request({
    method: HttpMethods.POST,
    path: `/v1/incidents/${emptyIncidentId}/assessments`,
    body: requestBody,
    auth: authentication
  })

And its giving the correct response. Now, I am trying to put the above into a separate async function because this gets repeated in many tests. So my function looks like below:

const response = doRequest(requestBody, emptyIncidentId, authentication)

And the function definition is :

async function doRequest (requestBody, incidentId, authentication) {
  return request({
    method: HttpMethods.POST,
    path: `/v1/incidents/${incidentId}/assessments`,
    body: requestBody,
    auth: authentication
  })

But this doRequest() call always responds with an empty response body. Could anyone tell me what I am doing wrong here. I checked the passed parameters, they are being passed correctly.

Upvotes: 1

Views: 20

Answers (2)

Jorg
Jorg

Reputation: 7250

The way to call an asynchronous function in this manner is to await it:

const response = await doRequest(requestBody, emptyIncidentId, authentication)

Upvotes: 1

Darshana Pathum
Darshana Pathum

Reputation: 669

When you call the function try it with await keyword. Because of the request is asynchronous

const response = await doRequest(requestBody, emptyIncidentId, authentication);

Upvotes: 1

Related Questions