How to mock mongoose schema using typescript and jest, what type to use for return?

Viewed 133

What should be the type for the object acting as the response for mongoose?

This is my mongoose mode/schema

import { model, Schema, Document } from "mongoose";

export interface ILocation extends Document{
  name: string;
  description: string;
}

const schema = new Schema({
  name: { type: String, required: true },
  description: { type: String, required: true },
});

export const Location = model<ILocation>(process.env.DB_CONTAINER || "Null", schema)

export default Location

I then have a create record method, like so,

const createRecord = async (
  obj: ILocation,
  model: IModel
): Promise<Response> => {
  try {
    const response = await model.create(obj);
    return { code: 201, data: response };
  } catch (error) {
    return errorHandler(error);
  }
};

This is fine, everything is working, but now I need to mock the response for a uunit test and I cant pass any object in as the fake response without TypeScript errors.

e.g.

const dummyRecord: ILocation = {
  name: "name",
  description: "description",
};

Location.create(dummyRecord);

// is missing the following properties increment, model and 54 more.ts

What should be the type for the dummy record or the model so that the return type can be my schema?

0 Answers
Related