I have this code;
import { initRepo, getOrganisationsRepo } from "Repositories";
import logger from "Logger";
export const getOrganisations = async (event) => {
try {
await initRepo();
const repo = await getOrganisationsRepo();
const data = await repo.getOrganisations();
// 200: Response is an array & is not empty
if (Array.isArray(data) && data.length)
return { statusCode: 200, body: JSON.stringify(data) };
// 404: Response is an array and is empty
if (Array.isArray(data) && !data.length) return { statusCode: 404 };
return { statusCode: 400 };
} catch (error) {
const errorMessage = error?.message || "error fetching organisations";
logger.error({ event: "org_get_failed" }, errorMessage);
return { statusCode: 400, body: errorMessage };
}
};
I want to mock the following calls using jest, as this is DB logic that I do not want to test here.
await initRepo();
const repo = await getOrganisationsRepo();
const data = await repo.getOrganisations();
And I would like to mock the return value of repo.getOrganisations();
I am not quite sure exactly how to do this. I can mock getOrganisationsRepo but then I'm not sure how to mock the return value of repo.getOrganisations()
This is pretty much as far as got with the mocks, these work, and I can test that the two functions have been called once, etc.
jest.mock("Repositories");
mockRepositories.initRepo.mockImplementation(() => Promise.resolve());
mockRepositories.getOrganisationsRepo.mockImplementation(() =>
Promise.resolve()
);