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

197
Visualizações
issue in testing expressjs routes by jest

I wrote a test for a route in Expressjs by jest and supertest. The express app first of all connects to MongoDB and Redis which both are in the cloud.

These are the versions of the packages:

 "devDependencies": {
    "jest": "^28.0.3",
    "supertest": "^6.2.3"
  }

This is the test I wrote:

const request = require("supertest")
const app = require("../src/app")

describe("Post Endpoints", () => {
  test("should create a new Job Seeker", async (done) => {
    try {
      const res = await request(app).post("/auth/register").send({
        email: "user@hamid.me",
        password: "1234",
      })
      expect(res.statusCode).toBe(200)
      done()
    } catch (e) {
      expect(e)
    }
  })
})

If I write the test without try/catch the test fail because of the app trying to connect to MongoDB and Redis.

This is the error I got:

 jest
  console.log
    Server running on port 3000

      at Server.log (src/app.js:42:11)

  console.log
    Client connected to Redis...

      at RedisClient.log (src/helpers/init_redis.js:10:11)

  console.log
    Client connected to Redis and ready to use...

      at RedisClient.log (src/helpers/init_redis.js:14:11)

  console.log
    Mongoose connected to db

      at NativeConnection.log (src/helpers/init_mongodb.js:17:11)

  console.log
    mongodb connected.

      at log (src/helpers/init_mongodb.js:12:13)

 FAIL  tests/Routes.test.js (8.168 s)
  Post Endpoints
    ✕ should create a new Job Seeker (5003 ms)

  ● Post Endpoints › should create a new Job Seeker

    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."

      3 |
      4 | describe("Post Endpoints", () => {
    > 5 |   test("should create a new Job Seeker", async (done) => {
        |   ^
      6 |     try {
      7 |       const res = await request(app).post("/auth/register").send({
      8 |         email: "user@hamid.me",

      at test (tests/Routes.test.js:5:3)
      at Object.describe (tests/Routes.test.js:4:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)
      at runJest (node_modules/@jest/core/build/runJest.js:407:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:338:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:190:3)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        8.288 s
Ran all test suites.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

I tried to this jest.setTimeout(new timeout) with different values but nothing happened.

about 4 years ago · Juan Pablo Isaza
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