'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: "[email protected]",
        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: "[email protected]",

      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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source