I am trying to write mock a test for the following function call
const contract = await new web3.eth.Contract();
tx.data = await contract.methods
.setup(
[juryAddress, buyerAddress, sellerAddress],
threshold,
to,
txData,
fallbackHandler,
paymentToken,
0,
sellerAddress
)
.encodeABI();
I am trying to mock this as a function contract.methods.setup() that returns a function object encodeABI() which then returns a dummy value {}.
The mock I am trying looks like this, although it is not working
const encodeABI = jest.fn()
encodeABI.mockReturnValue({})
const contract = {
methods: {
setup: jest.fn(),
}
}
eth.Contract.mockResolvedValue(contract)
contract.methods.setup.mockResolvedValue(encodeABI)
expect(encodeABI).toBeCalled()
expect(encodeABI).toBeCalled() is not being called as I expect
mockResolvedValue()returns aPromise. Are you sure you don't just wantcontract.methods.setup.mockReturnValue(encodeABI)instead?setup(), sincecontract.methods.setup()is async, right?contract.methods.setup(...).encodeABI(). If anything, it'sencodeABI()that should return aPromisesince that's what you are awaitingcontract.methods.setup()returns a value, and.encodeABI()returns the Promise.