0

For example if you have the module:

//sumWrapper.js
const sum = (a, b) => {
  return a + b;
};

const sumWrapper = (a, b) => {
  return sum(a, b);
};
export default sumWrapper;

and you're trying to test the sumWrapper function. However, you'd like to mock the sum function to

const sum = jest.fn((a, b) => a * b);

However, because this implementation was not given to sumWrapper when it was defined sum still does the original addition function. What would be the best way to mock this implementation and give sumWrapper access to the new multiplication sum mock function?

This is useful to me because I have a function getAddress() that gets the address of a smart contract depending on the current network. I'd like to mock the getNetwork function provided by web3 to return a network of my choosing to better test the code.

AviouslyAK
  • 107
  • 5
  • Does this answer your question? https://stackoverflow.com/questions/45111198/how-to-mock-functions-in-the-same-module-using-jest – Lin Du Nov 25 '21 at 02:28
  • Does this answer your question? [How to mock functions in the same module using Jest?](https://stackoverflow.com/questions/45111198/how-to-mock-functions-in-the-same-module-using-jest) – jonrsharpe Nov 25 '21 at 07:38
  • 1
    Specifically: https://stackoverflow.com/a/70066090/3001761. **TL;DR**: don't. – jonrsharpe Nov 25 '21 at 07:39

0 Answers0