How to increase code coverage for an asynchronous operations in Nodejs using Sonar, Jest and Supertest

210
January 19, 2020, at 10:10 PM

How can we increase code coverage for asynchronous operations in Nodejs using Sonar, Jest and Supertest. For the following code i wrote a test case and check the status as 200.

api.post('/activate', async (req, res, next) => {
  const { action, requestId } = req.body
  DeviceManager.ActivateDevices(req.body) //call device activation
  res.send({ requestId, message: 'Request is on processing', action, status: Config.STATUS.PENDING })
  next()
})

  test('should activate a device', async done => {
    const res = await request(app)
      .post('/api/v1/devices/activate')
      .send({
        mac:'YUINH6767'
        requestId: '791',
      })
    expect(res.statusCode).toEqual(200)
    done()
  })

Here the statement DeviceManager.ActivateDevices(req.body) is an async activity. This action will be complete at some point say for example after 1 min. In this case how to write a proper test case in order get a good code coverage.

Answer 1

Problem is that you are not waiting for async operation to get completed. You should await the async call before sending response to the user. something like

api.post('/activate', async (req, res, next) => {
  const { action, requestId } = req.body
  await DeviceManager.ActivateDevices(req.body) //call device activation
  res.send({ requestId, message: 'Request is on processing', action, status: Config.STATUS.PENDING })
  next()
})
Rent Charter Buses Company
READ ALSO
Axios data not displayed in Vuetify table

Axios data not displayed in Vuetify table

screenshotI am trying to display data(fetched from api through axios) in the vuetify tableAs shown in the screenshots 23 empty rows has been loaded, without data

189
Null value in GraphQL when trying to fetch data [duplicate]

Null value in GraphQL when trying to fetch data [duplicate]

Why am I receiving null value when I'm trying to fetch data from an external API?

154
How do I connect to an already existing SQLite database using node.js

How do I connect to an already existing SQLite database using node.js

Recently, I've been trying to work on adding a database to my Discord botI have been able to create the database and it is working perfectly fine, however, I want to make another command that has access to the existing database for any updates or changes...

177
Authenticating using ws WebSocket

Authenticating using ws WebSocket

I'm using ws webSocket (can't use socketio) and I'm a bit new to it

205