I have this simplified Jest test:
import login from "../../src/controller/auth/login";
import findUserByEmail from "../../src/api/db/find-user-by-email";
jest.mock("../../src/api/db/find-user-by-email");
const mockedfindUserByEmail = findUserByEmail as jest.Mocked<typeof findUserByEmail>;
describe("Login API", () => {
test('should return 401', async () => {
const req = {
email: "email@domain.com",
password: "password"
};
mockedfindUserByEmail.mockImplementation(async () => false)
expect(await login(req).status).toBe(401);
});
});
I got this mock from the docs. When I don't mock the test works fine because the login module connects to the database. But when I use the above mock I get:
TypeError: findUserByEmail.mockImplementation is not a function
Can you help?
The find-user-by-email file:
import { User } from "../../entities/main/user";
const db: any = require("../../db/models");
export default async (email: string)
: Promise<User> => {
return await db.user.findOne({
where: { email },
raw: true,
});
};