Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!