3

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.

Tauseef_Ahmed
  • 343
  • 3
  • 8
  • 18
  • 1
    looks like the copy of the current one https://stackoverflow.com/questions/21948296/mocha-supertest-econnreset – chavy Mar 31 '22 at 08:41
  • I'm already using supertest as explained in this post. It doesn't solve my issue. – Tauseef_Ahmed Apr 01 '22 at 13:55
  • Possible race condition? I was running into ECONNRESET issues when using request.post for api controllers that work flawlessly in postman and through our app. The ECONNRESET would occur when I was validating incoming parameters before proceeding with business logic. The only variant I could determine between which res.send() worked and didn't was time... so I inserted a 1000ms sleep and my ECONNRESET errors stopped. – rwheadon Mar 17 '23 at 14:15

1 Answers1

0

So others can benefit, in the comments @rwheadon mentioned it was a race condition for them.

Can confirm that was the case for us as well.

This is the final working code for us:

it('should fail without a valid token', (done) => {
  // After multiple hours of trying to get it to work, we figured out that
  // _in this specific test_, the file being consumed on the same tick as
  // the request being sent causes an issue where the server has an
  // ECONNRESET error.  This is not an issue normally, or in any other
  // test, only for this test.
  const file = fs.createReadStream(path.join(process.cwd(), uploadExcelFile));
  setTimeout(() => {
    localSuperTest
      .post(`/upload-redeem-codes`)
      .attach('file', file)
      .expect(401)
      .end((err: any, res: any) => {
        if (err) {
          return done(err);
        }
        assert.equal(res.body.message, 'not authorized');
        done();
      });
    });
  });

Somehow, there is a race condition in tests when using .attach() with supertest.

The main point, we believe, is that reading the file, and then consuming the file (with the .attach('file', file), have to be on two different ticks in the callback loop.

I hope that saves a poor soul several hours of investigation.

greduan
  • 4,770
  • 6
  • 45
  • 73