Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | /**
* Global Mongoose Model Mock Factory for Be-Vision Unit Tests.
*
* Usage:
* import { createMockModel, MockModel } from '../test-utils/mock-model.factory';
* const mockUserModel = createMockModel();
* { provide: getModelToken('User'), useValue: mockUserModel }
*/
export type MockModel<T = any> = {
find: jest.Mock;
findOne: jest.Mock;
findById: jest.Mock;
findByIdAndUpdate: jest.Mock;
findOneAndUpdate: jest.Mock;
create: jest.Mock;
updateMany: jest.Mock;
updateOne: jest.Mock;
deleteMany: jest.Mock;
countDocuments: jest.Mock;
aggregate: jest.Mock;
deleteOne: jest.Mock;
insertMany: jest.Mock;
db: {
startSession: jest.Mock;
};
};
/**
* Creates a chainable mock model that supports .lean().exec(), .skip().limit().sort().lean().exec(), etc.
* Also supports thenable queries to allow await without calling .exec() explicitly.
*/
export function createMockModel(overrides: Partial<MockModel> = {}): MockModel {
const chainable = (resolvedValue: any = null) => {
const obj: any = {
lean: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue(resolvedValue),
skip: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
sort: jest.fn().mockReturnThis(),
select: jest.fn().mockReturnThis(),
populate: jest.fn().mockReturnThis(),
session: jest.fn().mockReturnThis(),
save: jest.fn().mockResolvedValue(resolvedValue),
};
// Make the query object thenable so `await model.find()` works natively in tests.
obj.then = (onfulfilled: any) =>
Promise.resolve(resolvedValue).then(onfulfilled);
return obj;
};
const mockSession = {
startTransaction: jest.fn(),
commitTransaction: jest.fn().mockResolvedValue(undefined),
abortTransaction: jest.fn().mockResolvedValue(undefined),
endSession: jest.fn(),
};
return {
find: jest.fn().mockImplementation(() => chainable([])),
findOne: jest.fn().mockImplementation(() => chainable(null)),
findById: jest.fn().mockImplementation(() => chainable(null)),
findByIdAndUpdate: jest.fn().mockImplementation(() => chainable(null)),
findOneAndUpdate: jest.fn().mockImplementation(() => chainable(null)),
create: jest.fn().mockResolvedValue([]),
updateMany: jest
.fn()
.mockImplementation(() => chainable({ modifiedCount: 0 })),
updateOne: jest
.fn()
.mockImplementation(() => chainable({ modifiedCount: 0 })),
deleteMany: jest
.fn()
.mockImplementation(() => chainable({ deletedCount: 0 })),
countDocuments: jest.fn().mockImplementation(() => chainable(0)),
aggregate: jest.fn().mockImplementation(() => chainable([])),
deleteOne: jest
.fn()
.mockImplementation(() => chainable({ deletedCount: 0 })),
insertMany: jest.fn().mockResolvedValue([]),
db: {
startSession: jest.fn().mockResolvedValue(mockSession),
},
...overrides,
};
}
/**
* Helper: Make a chainable query resolve to a specific value.
*/
export function mockChainResolve(mockFn: jest.Mock, value: any): void {
const obj: any = {
lean: jest.fn().mockReturnThis(),
exec: jest.fn().mockResolvedValue(value),
skip: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
sort: jest.fn().mockReturnThis(),
select: jest.fn().mockReturnThis(),
populate: jest.fn().mockReturnThis(),
session: jest.fn().mockReturnThis(),
save: jest.fn().mockResolvedValue(value),
};
obj.then = (onfulfilled: any) => Promise.resolve(value).then(onfulfilled);
mockFn.mockReturnValue(obj);
}
|