#testing express using jest and mongoose

5 messages · Page 1 of 1 (latest)

thick pike

anyone knows a solution to get rid of my errors?

import {createApp} from "@/app";
import mongoose from "@/database/mongoose";

describe("GET /api/user", () => {
    beforeAll(async () => {
        await mongoose.run()
        const con = await mongoose.connection()
        const app = createApp(con);
    })
//
    afterAll(async () => {
        //Problem here!
        await mongoose.stop()
        // server.close();

    })
    it("should return a user", async () => {
        // const res = await supertest(app).get("/api/user");
        // expect(res.statusCode).toBe(200);
        expect(1).toBeGreaterThan(0);
    });
});

if I dont use the await mongose.stop(); i get this error

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TCPWRAP

       8 |         try {
       9 |             if (process.env.NODE_ENV === "test") {
    > 10 |                 return await connect(process.env.MONGO_URI_TEST)
         |                                     ^
      11 |
      12 |             }
      13 |             return await connect(process.env.MONGO_DASHBOARD_URI)

blazing bridgeBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
thick pike

if I call await mongoose.stop(); i get this error

Test suite failed to run

    MongoPoolClosedError: Attempted to check out a connection from closed connection pool

      10 | //
      11 |     afterAll(async () => {
    > 12 |         await mongoose.stop()
         |         ^
      13 |         // server.close();
      14 |
      15 |     })

i really dont know what to do

prime sentinel

But that error clearly shows you do call mongoose.stop()Thonk