Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
supertest times out on post request

i'm trying to use supertest to test some express routes, however the test throws a timeout error when i try to send post requests despite the get requests actually working.

this is the route i'm testing

router.post('/', async (req, res) => {
  try {
    const { command, text } = req.body;

    switch (command) {
      case 'abc': return;
      default:
        console.log('123');
    }
  } catch (error) {
    console.error(error);
  }
});

this is the test

describe('test', () => {
  const app: express.Express = express();
  app.use('/', route);
  app.use(express.urlencoded({ extended: true }));

  it('test', async () => {
    await request(app)
      .post('/')
      .send('command=john') 

    expect(handleSurveyListCommand).not.toHaveBeenCalled();
    expect(handleSurveyRespondCommand).not.toHaveBeenCalled();
    expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
    expect(consoleErrorSpy).toHaveBeenCalledWith(new Error('Command not found'));
  });
});

and this is the error thrown

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

this test fails even if the only thing i do inside the route is to log something.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is because it is not responding with the response while the request in the test case is waiting for some response, instead it tries to send the response.

 router.post('/', async (req, res) => { try { const { command, text } = req.body; switch (command) { case 'abc': res.status(200).send("abc"); default: res.status(200).send("123"); } } catch (error) { res.status(500).send("unknown error"); } });

Regarding undefined req.body -> use parsed body in application

 app.use(express.bodyParser());
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda