Skip to content

Commit 9af632d

Browse files
committed
connectMongoose testing ok
1 parent 458db07 commit 9af632d

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
coverage/

nodeapp/lib/connectMongoose.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
});

nodeapp/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"start": "cross-env NODEAPP_ENV=production node server.js",
99
"initDB": "node initDB.js",
1010
"test": "jest",
11-
"test:watch": "jest --watch"
11+
"test:watch": "jest --watch",
12+
"test:coverage": "jest --coverage --coverageDirectory=test-coverage"
1213
},
1314
"keywords": [],
1415
"author": "",

0 commit comments

Comments
 (0)