In my code, there is a function called processCosts
located in the file prepareStatement.ts
. This function makes a call to another function named calculatePrice
, which is imported from coreLogic.ts
.
Within my test file reports.integration.ts
, I have imported processCosts
, but I need to mock the behavior of calculatePrice
, which is being called by processCosts
. To achieve this, I created a new file coreLogic.ts
within a __mocks__
folder with the following content:
export const calculatePrice = jest.fn(() => {});
Next, in my test file, outside of the it(...)
block but within the describe(...)
block, I added the line:
jest.mock('../statements/prepareStatement');
Finally, the test case itself looks like this:
it('should calculate processCost and be 4 times the value returned by calculatePrice', async () => {
(calculatePrice as jest.Mock).mockImplementationOnce(() => 100.00);
expect(processCost).to.equal(400.00); // processCost will be 4 times the value returned by calculatePrice
}
However, when running the code, I encountered the error message:
TypeError: coreLogic_1.calculatePrice.mockImplementationOnce is not a function
I am seeking guidance on where I may have made a mistake. It seems that others who have attempted a similar approach are calling the method they are mocking directly in their test cases, as opposed to within another imported function. After debugging the code, the issue appears to be related to the following line:
(calculatePrice as jest.Mock).mockImplementationOnce(() => 100.00);