How would one mock this call to a ethereum smart contract using the web3 js library.
export const getERC20Balance = async (
web3Client: Web3,
tokenAddress: string,
walletAddress: string
) => {
const balanceOfAbi = getERC20ABIOfInterest('balanceOf');
const contract = new web3Client.eth.Contract(balanceOfAbi, tokenAddress);
const result = await contract.methods.balanceOf(walletAddress).call();
if (!result) return;
const resultInEther = web3Client.utils.fromWei(Number(result), 'ether');
return resultInEther
};
more specifically, const contract = new web3Client.eth.Contract(balanceOfAbi, tokenAddress); const result = await contract.methods.balanceOf(walletAddress).call();
I've tried mocking the library using mockDeep but get TypeError: Cannot read properties of undefined (reading 'balanceOf')
jest.mock('web3', () => { const web3 = jest.requireActual('web3'); jest.spyOn(web3, 'Contract').mockReturnValue({ Contract: () => ({ methods: jest.fn().mockImplementation(() => ({ // eslint-disable-next-line @typescript-eslint/no-unused-vars balanceOf: (_walletAddress: string) => ({ call: jest.fn(), }), })), }), });