supertest failing with ECONNRESET

Viewed 198

I'm working on a Node.js project and trying to write test cases for the app. The endpoints are working perfectly fine when I run them in Postman. But when I run the test cases, some of them, (Usually the first one in the order) fails with the error

 - Error: read ECONNRESET

I have tried debugging it, searched for a potential solution, but I'm unable to fix it. I'm working with jasmine/supertest for the first time. Maybe I'm missing something I'm unable to anticipate but I haven't been able to find out the issue. Can anybody help me out with this? Thanks a bunch in advance.

Here is my test code.

 it('Gets Users List', async() => {
     const response = await request.get('/users')
    

    expect(response.status).toBe(200);
    
})

and my Controller function

export const index = async (req:Request, res: Response) => {
try {
    const users : DataObject = await model.index()
    
    res.status(users.status);
    res.json(users.data);

  } catch (error) {
    res.status(NOT_FOUND);
    res.json(error);
  }
}

The peculiar behavior that I observed is when I add another method before this one, then this method works fine and the first one returns the same error. I assume the first call is rejected due to the fact that connection was not established yet? I'm not sure though.

Anybody familiar with this issue?

Thanks in advance.

0 Answers
Related