Escribí una prueba para una ruta en Expressjs por broma y superprueba. La aplicación Express, en primer lugar, se conecta a MongoDB y Redis, que están en la nube.
Estas son las versiones de los paquetes:
"devDependencies": { "jest": "^28.0.3", "supertest": "^6.2.3" }Esta es la prueba que escribí:
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) } }) })Si escribo la prueba sin probar/atrapar, la prueba falla debido a que la aplicación intenta conectarse a MongoDB y Redis.
Este es el error que obtuve:
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. Intenté este jest.setTimeout(new timeout) con diferentes valores pero no sucedió nada.