I'm a Jest newbie and I've hit a roadblock trying to mock a module that includes both a Class ("Client") and a function ("getCreds"). The Class Client has a method called Login. Here's the code snippet I want to test:
import * as sm from 'some-client';
const smCli: sm.Client = new sm.Client();
export const getKey = async (): void => {
const smCreds = await sm.getCreds();
await smCli.login(smCreds);
};
The issue I'm facing is that while I can mock the getCreds function easily, I'm not sure how to mock the login function of the Client instance in order to properly test the getKey function. I've tried different approaches like the one below, but none seem to work. Can anyone point out where I'm going wrong? Thank you.
import * as sm from 'some-client';
jest.mock('some-client');
const smClientMock = sm.Client as jest.Mock<unknown>
const smGetCredsMock = sm.getCreds as jest.Mock<Promise<unknown>>
smGetCredsMock.mockResolvedValue(1);
smClientMock.mockImplementation(() => {
return {
login: () => {
return 2;
}
};
});