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

375
Visualizações
Testing default error handler in an express application results in a timeout

When testing the default error handler of an express application, it results in a timeout. The function looks as follows:

const createApp = (underlyingFunction) => {
  const app = express()
  app.get('/my-endpoint', async (req, res) => {
    await underlyingFunction()
    res.send({ success: true })
  })

  const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
    console.error('Unhandled exception');
    console.error(error);
    console.error(error.stack);
    res.status(500).send({
      message: 'Oh dear',
    });
    // next()
  }

  app.use(errorHandler)
  return app;
}
  

And the test looks as follows:

  test('error should be handled and return 500', async () => {
    underlyingFunction.mockImplementation(() => {
      throw new Error('Something went wrong')
    })

    const app = createApp(underlyingFunction)
    const response = await request(app).get('/my-endpoint')

    expect(response.status).toBe(500)
  })

When running the test, I get the following error:

    thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

What could be causing this?

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

0

For express V4, from the Error Handling#Catching Errors doc, we know:

For errors returned from asynchronous functions invoked by route handlers and middleware, you must pass them to the next() function, where Express will catch and process them.

Although the underlyingFunction of the mock in the test case is synchronous, but in the route, the async/await syntax converts this route handler into asynchronous code.

So, you need to use try...catch statement to catch the error raised from underlyingFunction function. And pass the error to the next function. express will route the request to the error handler middleware with that error.

E.g.

app.ts:

import express from 'express';
import { ErrorRequestHandler } from 'express-serve-static-core';

export const createApp = (underlyingFunction) => {
  const app = express();

  app.get('/my-endpoint', async (req, res, next) => {
    try {
      await underlyingFunction();
      res.send({ success: true });
    } catch (error) {
      next(error);
    }
  });

  const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
    console.error('Unhandled exception');
    res.status(500).send({ message: 'Oh dear' });
  };

  app.use(errorHandler);
  return app;
};

app.test.ts:

import request from 'supertest';
import { createApp } from './app';

describe('68923821', () => {
  test('error should be handled and return 500', async () => {
    const underlyingFunction = jest.fn().mockImplementation(() => {
      throw new Error('Something went wrong');
    });
    const app = createApp(underlyingFunction);
    const res = await request(app).get('/my-endpoint');
    expect(res.status).toEqual(500);
  });
});

test result:

 PASS  examples/68923821/app.test.ts (9.128 s)
  68923821
    ✓ error should be handled and return 500 (49 ms)

  console.error
    Unhandled exception

      15 | 
      16 |   const errorHandler: ErrorRequestHandler = (error, req, res, next) => {
    > 17 |     console.error('Unhandled exception');
         |             ^
      18 |     res.status(500).send({ message: 'Oh dear' });
      19 |   };
      20 | 

      at errorHandler (examples/68923821/app.ts:17:13)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        9.661 s
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