I'm looking for some help with mocking the BigNumber library for some Jest tests.
In my webapp I have some BigNumber calculations such as:
const a = '30';
const b = '20';
const BigNA = BigNumber.from(a);
const BigNB = BigNumber.from(b);
const conversion = BigNumber.from(Math.pow(10, 12));
const AMinusB = BigNA.sub(BigNB);
const AMinusBConversion = AMinusB.div(conversion)
The above is just a made up example but closely mirrors what is happening in my codebase. Now to test this I need to mock the BigNumber library so that it is always returned with the from, sub, div, gt etc methods on it, so the next BigNumber calculation can be performed.
In my tests I have the following:
const BigNumber: any = {
from: () => BigNumber
sub: () => BigNumber,
gt: () => true,
div: () => jest.fn(() => '10'),
};
jest.mock('@ethersproject/bignumber', () => ({
BigNumber,
}));
I have also tried many variations of the above mock such as:
from: () => jest.mock(() => BigNumber),
from: () => ({ ...BigNumber })
However in all cases the test either hangs out entirely, or throws a BigNA.sub is not a function error when it hits the sub or div calls.
Would be really appreciated if anyone has any insight or pointers in regards to mocking this BigNumber library as I'm entirely out of ideas at this point.