Currently, I am in the process of writing a test case for one of my functions. This function calls another function from a library, and I am attempting to mock this function (saveCall). Below is a snippet of the sample code in question:
import { Call } from './somefolder/call';
class Demo {
var testIt = (params: any) => {
---- // Some other code
let call = new Call(params);
call.saveCall();
---- // Some other code
}
return {testIt: testIt};
}
Furthermore, here is my approach to writing a unit test case for the function:
import { Call } from './somefolder/call';
var demo = new Demo();
test("Test it", () => {
let call = new Call({} as any);
let spyIt = jest.spyOn(call, 'saveCall').mockImplementation(()=>{console.log('here')});
demo.testIt();
expect(spyIt).toHaveBeenCalled(); // Throws error expect(jest.fn()).toHaveBeenCalled()
});
Currently, I am encountering an error with the
expect(jest.fn()).toHaveBeenCalled()
statement. It seems like the error is occurring because the instance of the call object in the test file differs from the one in the Demo class. This discrepancy is causing the spyOn function to be unable to determine whether the function has been called or not. I did attempt to mock the entire Call.ts file, but the error persists.
Given this situation, my query is how can I effectively create a mock and verify whether saveCall()
has been called without altering the implementation of the testIt
function.