Skip to content

Commit

Permalink
fix mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuyog2 committed Aug 13, 2024
1 parent df3cc59 commit c09eb0a
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 91 deletions.
90 changes: 45 additions & 45 deletions junit.xml

Large diffs are not rendered by default.

42 changes: 0 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions test/__mocks__/mongoose.js
Original file line number Diff line number Diff line change
@@ -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<string>} 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<string>} 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;
3 changes: 2 additions & 1 deletion test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 2 additions & 1 deletion test/middlewares.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion test/role.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
3 changes: 2 additions & 1 deletion test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c09eb0a

Please sign in to comment.