-1

I wanted to mock the database response from the API request. I am trying to apply Manual Mocks, but it does not work. Every time responds from DB, not from mocks. How can I fixed that? here is my code:

app.test.ts

jest.mock('../services/__mocks__/image');

describe("It shuld be get methoad", () => {
  it("get all image url", async () => {
    const res = await request(app).get("/api/v1/all");
    expect(res.statusCode).toBe(200);
    // console.log(res.body);
    let images = res.body
    // expect(images.length).toBeGreaterThan(0);
    expect(images.length).toBe(1);
  });
});

services/mocks/image.ts

let images = [
  {
    "_id": "61364c4552781af510baf4bf",
    "title": "9-06_09_2021_11_13_41",
    "imageURL": "upload/9-06_09_2021_11_13_41.jpeg",
    "imageFullURL": "http://localhost:4000/9-06_09_2021_11_13_41.jpeg",
    "createdAt": "2021-09-06T17:13:41.390Z"
  }
];

export const allImages = () => {
  console.log("mock used");
  return images;
};

services/image.ts

import Image from "../models/image";

export const allImages = async () => {
    console.log("main used");
    const all = await Image.find();
    return all;
}

1 Answers1

0

Instead of mocking, I got a solution using the mongodb-memory-server package.