I am currently working with mockingoose 2.13.2 and mongoose 5.12.2, leveraging Typescript and jest for testing purposes. Within my test scenario, I am attempting to mock a call to my schema's find
method. Here is what I have tried:
import mockingoose from 'mockingoose';
...
beforeEach(async () => {
jest.resetAllMocks();
jest.clearAllMocks();
mockingoose(File).reset();
console.log("mock response:" + JSON.stringify(fileMockResponse));
mockingoose(File).toReturn(fileMockResponse, 'find');
const filePostList = await File.find({
_id: { $in: ['test'] },
});
console.log("mocking file post list:" + JSON.stringify(filePostList));
However, upon test execution, the following output is logged:
mock response:[{"data:" ... }]
at Suite.<anonymous> (routes/file.test.ts:237:15)
console.log
mocking file post list:undefined
The appearance of undefined
indicates that my attempt to mock a response from the find
call was unsuccessful.
Here is a snippet of what my model/schema entails:
export interface IFile extends Document {
author: string;
...
}
const FileSchema: Schema = new Schema(
{
author: { type: String, required: false },
...
}
export default mongoose.model<IFile>('File', FileSchema);