I have the following code snippet:
async myFunction(paramA?: string): Promise<void> {
if (paramA === undefined) {
paramA = this.generateRandomString();
}
this.funnel.loginFunnel(Status.Pending);
await this.tracker.flush();
this.service.call(this.name, paramA, code);
}
I want to test that loginFunnel is called with status pending and the service is called with paramA. However, these classes are initialized in the constructor:
constructor(params: Params) {
this.tracker = new Tracker(params);
this.service = new Service(params, this.tracker);
}
How can I spy on them using Jest? This is pure JavaScript, not related to React or any other similar framework.
I've tried multiple approaches but haven't been successful...
My latest attempt involved importing the Tracker class from its path:
jest.mock('../tracker');
service.call();
expect(Tracker).toHaveBeenCalledTimes(1);
However, the test returned the following error:
expect(received).toHaveBeenCalledTimes(expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function Tracker]