diff --git a/junit.xml b/junit.xml index d92ca3f..d4d7dca 100644 --- a/junit.xml +++ b/junit.xml @@ -1,81 +1,81 @@ - - - + + + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -85,45 +85,45 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/package-lock.json b/package-lock.json index 42fb32e..78ecc79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,6 @@ "globals": "^15.9.0", "jest": "^29.7.0", "jest-junit": "^16.0.0", - "mongoose-mock": "^0.4.0", "node-mocks-http": "^1.15.1", "nodemailer-mock": "^2.0.6", "supertest": "^7.0.0", @@ -3745,16 +3744,6 @@ "node": ">= 6" } }, - "node_modules/formatio": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/formatio/-/formatio-1.0.2.tgz", - "integrity": "sha512-db27e1R5chXs7pZcJAh7aPC5iJRKRuCJEUF0cFWBdfG/Q0ZMehLg+dbibkXU5uyNM5AjmwBma8PkloxR+l2I1w==", - "deprecated": "This package is unmaintained. Use @sinonjs/formatio instead", - "dev": true, - "dependencies": { - "samsam": "~1.1" - } - }, "node_modules/formidable": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.1.tgz", @@ -5706,16 +5695,6 @@ "url": "https://opencollective.com/mongoose" } }, - "node_modules/mongoose-mock": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/mongoose-mock/-/mongoose-mock-0.4.0.tgz", - "integrity": "sha512-PHYL3HA+hl7rEysnTwiBVna3ZjPhE98dNDPsclkJ9BbGBLP5PBW8fIN5eAR1+mQM2bbubIadKrzcFZbs4/dOtQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "sinon": "~1.8.1" - } - }, "node_modules/mongoose/node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -6558,14 +6537,6 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "license": "MIT" }, - "node_modules/samsam": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/samsam/-/samsam-1.1.3.tgz", - "integrity": "sha512-t9rCPskf50hZ53eH8Z+cSWD4LfJBac+8vSSuzi1Y2HzygyXxtAl0BaR3hr6iI6A+nFQbkmJNC/brQLNEeVnrmg==", - "deprecated": "This package has been deprecated in favour of @sinonjs/samsam", - "dev": true, - "license": "BSD-3-Clause" - }, "node_modules/schema-utils": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.3.0.tgz", @@ -6743,19 +6714,6 @@ "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "license": "ISC" }, - "node_modules/sinon": { - "version": "1.8.2", - "resolved": "https://registry.npmjs.org/sinon/-/sinon-1.8.2.tgz", - "integrity": "sha512-lKW2kN7YeAOGbqLAMkT/aDSix9ZYZsNAMTmyqP9GHGFK3gDwL/htfK2Aa/jc2K6rtdI2Y0ekrRzKi5J2ZG99kw==", - "deprecated": "16.1.1", - "dev": true, - "dependencies": { - "formatio": "~1.0" - }, - "engines": { - "node": ">=0.1.103" - } - }, "node_modules/sisteransi": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", diff --git a/test/__mocks__/mongoose.js b/test/__mocks__/mongoose.js new file mode 100644 index 0000000..bef6326 --- /dev/null +++ b/test/__mocks__/mongoose.js @@ -0,0 +1,109 @@ +/** + * Mock implementation of the Mongoose library for testing purposes. + * This mock replaces Mongoose's actual methods with Jest mocks to avoid + * interacting with a real MongoDB database during tests. + * + * @module __mocks__/mongoose + */ + +const mongoose = { + /** + * Mock implementation of the `connect` method. + * Resolves with 'Mocked Connection' to simulate a successful connection. + * + * @function + * @returns {Promise} A promise that resolves to 'Mocked Connection'. + */ + connect: jest.fn().mockResolvedValue('Mocked Connection'), + + /** + * Mock implementation of the `disconnect` method. + * Resolves with 'Mocked Disconnection' to simulate a successful disconnection. + * + * @function + * @returns {Promise} A promise that resolves to 'Mocked Disconnection'. + */ + disconnect: jest.fn().mockResolvedValue('Mocked Disconnection'), + + /** + * Mock model for `user` collection. + * Provides mock implementations of CRUD operations on the `user` model. + * + * @property {function} find - Mock implementation of `find` method. + * @property {function} findOne - Mock implementation of `findOne` method. + * @property {function} create - Mock implementation of `create` method. + * @property {function} findById - Mock implementation of `findById` method. + */ + user: jest.fn().mockReturnValue({ + find: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue(null), + create: jest.fn().mockResolvedValue({}), + findById: jest.fn().mockResolvedValue({}), + }), + + /** + * Mock model for `role` collection. + * Provides mock implementations of CRUD operations on the `role` model. + * + * @property {function} find - Mock implementation of `find` method. + * @property {function} findOne - Mock implementation of `findOne` method. + * @property {function} create - Mock implementation of `create` method. + * @property {function} findById - Mock implementation of `findById` method. + */ + role: jest.fn().mockReturnValue({ + find: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue(null), + create: jest.fn().mockResolvedValue({}), + findById: jest.fn().mockResolvedValue({}), + }), + + /** + * Mock model for `session` collection. + * Provides mock implementations of CRUD operations on the `session` model. + * + * @property {function} find - Mock implementation of `find` method. + * @property {function} findOne - Mock implementation of `findOne` method. + * @property {function} create - Mock implementation of `create` method. + * @property {function} findById - Mock implementation of `findById` method. + */ + session: jest.fn().mockReturnValue({ + find: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue(null), + create: jest.fn().mockResolvedValue({}), + findById: jest.fn().mockResolvedValue({}), + }), + + /** + * Mock model for `user_roles` collection. + * Provides mock implementations of CRUD operations on the `user_roles` model. + * + * @property {function} find - Mock implementation of `find` method. + * @property {function} findOne - Mock implementation of `findOne` method. + * @property {function} create - Mock implementation of `create` method. + * @property {function} findById - Mock implementation of `findById` method. + */ + user_roles: jest.fn().mockReturnValue({ + find: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue(null), + create: jest.fn().mockResolvedValue({}), + findById: jest.fn().mockResolvedValue({}), + }), + + /** + * Mock model for `log` collection. + * Provides mock implementations of CRUD operations on the `log` model. + * + * @property {function} find - Mock implementation of `find` method. + * @property {function} findOne - Mock implementation of `findOne` method. + * @property {function} create - Mock implementation of `create` method. + * @property {function} findById - Mock implementation of `findById` method. + */ + log: jest.fn().mockReturnValue({ + find: jest.fn().mockResolvedValue([]), + findOne: jest.fn().mockResolvedValue(null), + create: jest.fn().mockResolvedValue({}), + findById: jest.fn().mockResolvedValue({}), + }), +}; + +module.exports = mongoose; diff --git a/test/auth.test.js b/test/auth.test.js index b10769d..608e97d 100644 --- a/test/auth.test.js +++ b/test/auth.test.js @@ -34,7 +34,8 @@ const authController = require('./../controllers/auth.controller'); jest.mock('bcrypt'); jest.mock('jsonwebtoken'); jest.mock('../utils/emailService'); -jest.mock('mongoose', () => require('mongoose-mock')); +jest.mock('mongoose'); +jest.mock('../models', () => require('./__mocks__/mongoose')); /** * @description Unit tests for the login function of the auth controller. */ diff --git a/test/middlewares.test.js b/test/middlewares.test.js index 599f0e4..55b4f07 100644 --- a/test/middlewares.test.js +++ b/test/middlewares.test.js @@ -27,7 +27,8 @@ const jwt = require('jsonwebtoken'); const db = require('../models'); jest.mock('express-validator'); -jest.mock('mongoose', () => require('mongoose-mock')); +jest.mock('mongoose'); +jest.mock('../models', () => require('./__mocks__/mongoose')); /** * @description Unit tests for the verifyToken middleware function. * This middleware verifies JWT tokens, checks for token validity, blacklist status, and associated user information. diff --git a/test/role.test.js b/test/role.test.js index 2000708..d136c9a 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -24,7 +24,8 @@ const roleController = require('../controllers/role.controller'); const db = require('../models'); const httpMocks = require('node-mocks-http'); -jest.mock('mongoose', () => require('mongoose-mock')); +jest.mock('mongoose'); +jest.mock('../models', () => require('./__mocks__/mongoose')); describe('Role Controller', () => { beforeEach(() => { diff --git a/test/user.test.js b/test/user.test.js index 862493c..652b56d 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -28,7 +28,8 @@ const userController = require('../controllers/user.controller'); const db = require('../models'); const bcrypt = require('bcrypt'); const httpMocks = require('node-mocks-http'); -jest.mock('mongoose', () => require('mongoose-mock')); +jest.mock('mongoose'); +jest.mock('../models', () => require('./__mocks__/mongoose')); describe('User Controller', () => { let userMock;