ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

supertest.end function causing timeout

gfkauer opened this issue · comments

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.

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();

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.