I have a mongoose model, setup as per below,
// Model.TS
export interface LocationInterface extends mongoose.Document {
name: string;
description: string;
}
export const Location = mongoose.model<LocationInterface>(
process.env.DB_CONTAINER || "Null",
locationSchema
);
I then Mock it with,
https://jestjs.io/docs/en/mock-function-api
//tests.ts
Import Location from ‘./model’;
Location.create = jest.fn();
Then I want to confirm when it sends a json response its recieved ok.
it("should return json body in response", () => {
Location.create. // => no mockReturnValue method is avaliable here??
I have tried the below but it does not work. How can I mock an ES6 module import using Jest?
jest.mock("./model", () => ({
Location: jest.fn()
}));
I have tried to use export default Location, which also does not work.
I have tried the below, which also did not work.
jest.mock("../../../src/models/location.model", () => Location.create);
I tried the below from https://codewithhugo.com/jest-mock-spy-module-import/
import * as mockDB from "./model";
jest.mock('.model', () => ({
get: jest.fn(),
set: jest.fn()
}));
expect(mockDb.Location.create. // -> No Methods available