|
| 1 | +import connectMongoose from "./connectMongoose.js"; |
| 2 | +import mongoose, { connection } from "mongoose"; |
| 3 | + |
| 4 | +jest.mock("mongoose"); |
| 5 | + |
| 6 | +describe("connectMongoose", () => { |
| 7 | + /* |
| 8 | + //1.1 Versión Clasica. Conexión directa a MongoDB: |
| 9 | + it("Deberia devolver una promesa que resuelve una conexion", async () => { |
| 10 | + const connection = await connectMongoose(); |
| 11 | + expect(connection).toBeDefined(); |
| 12 | + expect(connection.name).toBeDefined(); |
| 13 | + await connection.close(); |
| 14 | + }); |
| 15 | + */ |
| 16 | + |
| 17 | + mongoose.connect.mockResolvedValue({ |
| 18 | + connection: { |
| 19 | + name: "mockName", |
| 20 | + close: jest.fn(), |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + let connection; |
| 25 | + beforeEach(async () => { |
| 26 | + connection = await connectMongoose(); |
| 27 | + }); |
| 28 | + |
| 29 | + //1.2 Versión con mocks: |
| 30 | + it("Deberia devolver una promesa que resuelve una conexion", async () => { |
| 31 | + //const connection = await connectMongoose(); |
| 32 | + expect(connection).toBeDefined(); |
| 33 | + expect(connection.name).toBeDefined(); |
| 34 | + await connection.close(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("Deberia llamar a mongoose.connect al iniciar una conexion", async () => { |
| 38 | + //const connection = await connectMongoose(); |
| 39 | + const spy = jest.spyOn(mongoose, "connect"); |
| 40 | + expect(spy).toHaveBeenCalled(); |
| 41 | + await connection.close(); |
| 42 | + }); |
| 43 | +}); |
0 commit comments