I am testing some of my code using Jest. When I try to mock a function (autogenerated by pgtyped), the test fails, saying the function returns undefined
. My original function is in: src/db/models/calls.ts
, and my mocked function is in src/db/models/__mocks__/calls.ts
.
To try to better understand the issue I am facing, I am trying to get the mocked function to simply console.log
the parameters it receives. Below is the mocked function:
export const getCallBySid = {
run: jest.fn().mockImplementation((params: { sid: string }) => {
console.log("getCallBySid params", params);
}),
};
What makes this even more confusing is, when I replace jest.fn().mockImplementation
with a direct implementation in the mock file, the console.log works. Below is the code that is able to console.log
export const getCallBySid = {
run: (params: { sid: string }) => {
console.log("getCallBySid", params);
},
};
I can’t tell why this would work, whereas the jest mock would not. I want to be able to get information on calls to the function, so it would be very helpful to be able to have it be a jest.fn in the mock.