I want to unit test my functions, but I have stopped when creating a mock to simulate the creation of a resource, I am using mongoose in a feathers project (nodejs). To do the unit tests I use the jest plugin. Here the function I want to test.
export async function saveCar(a: string, b: string) {
const carModel = mongoose.model('car');
const car = new carModel({ a, b});
await car.save();
}
To simulate the search by id I have used this mock:
jest.mock('mongoose', () => ({
model () {
return {
findById () {
return Promise.resolve(undefined);
},
};
},
}));
The function I use for this is:
export async function getCar (id: string) {
const carModel = mongoose.model('car');
return await carModel.findById(id);
}