Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supertest.end function causing timeout #785

Open
gfkauer opened this issue Aug 23, 2022 · 2 comments
Open

supertest.end function causing timeout #785

gfkauer opened this issue Aug 23, 2022 · 2 comments

Comments

@gfkauer
Copy link

gfkauer commented Aug 23, 2022

Hi,

I'm trying to implement some testes on a new project, but when i invoke the end function i have a timeout on return.

Test file

const supertest = require('supertest');
const chai = require('chai');
const app = require('../../../src/app');

const request = supertest(app);

describe('Routes: Users', () => {

    const defaultUser = {
        username: 'DefaultUser',
        email: 'defaultuser@email.com',
        password: '12345'
    };

    describe('GET /users', () => {
        it('should return a list of users', done => {
            request
                .get('/users')
                .end((err, res) => {
                    console.log("error");
                    done(err);
                });
        });
    });
});

Route file

const express = require('express');
const UsersController = require('../controllers/users');

const router = express.Router();
const usersController = new UsersController();

router.get('/', (req, res) => res.send([{
    name: 'Default product',
    description: 'product description',
    price: 100
}]));

module.exports = router;

Error log:

npm run test:integration

> card-collector-api@0.0.1 test:integration
> mocha --config test/integration/.mocharc.json test/integration/**/*_spec.js



  Routes: Users
    GET /users
      1) should return a list of users


  0 passing (2s)
  1 failing

  1) Routes: Users
       GET /users
         should return a list of users:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/workspace/test/integration/controllers/users_spec.js)
      at listOnTimeout (node:internal/timers:564:17)
      at process.processTimers (node:internal/timers:507:7)

Another curious think is that I to use ctrl+C command to end my test run.

@rudolfolah
Copy link

I'm running into the same error. What's interesting is that the get() call works without calling .end() and I get a different error about end being called twice if I add it. In my code I'm using the async/await style.

However, for the post() request, when setting the headers, calling .end() is what fixed the specific timeout error for me:

// This causes a timeout
const response = await request(app)
  .post("/webapp")
  .send({ some: "data" })
  .set('Accept', 'application/json')
  .set("Authorization", `Bearer ${token}`);

// Adding the "end" call resolves the issue
const response = await request(app)
  .post("/webapp")
  .send({ some: "data" })
  .set('Accept', 'application/json')
  .set("Authorization", `Bearer ${token}`)
  .end();

@rudolfolah
Copy link

Update on this, I ended up finding a bug in my POST handler code. There was an error being returned that would normally be handled by a middleware that would turn it into a response. The timeout was occurring because that middleware was not loading. There was no need for .end() once I fixed the issue.

It could be a middleware is missing or a call to send a response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants