I am having the below Node JS with mongoose code. I have to mock this query to throw an error in my JEST unit testing. Can anyone help me with how to achieve it?
return new Promise(async (resolve, reject) => {
try {
const data = await users
.find(filter, porjectFields)
.populate({
path: 'user.address',
populate: {
path: 'location'
}
})
.lean();
resolve(data);
} catch (err) {
logger.error(
`Error in user : ${err}`
);
reject(err);
}
});
I tried to mock like below, but i am getting error on lean function.
users.find = jest.fn().mockImplementation(() => ({
populate: jest.fn().mockReturnValue({ name: 'hello' }),
lean: true
}));
Error:
TypeError: users.find(...).populate(...).lean is not a function