Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Mocking BigNumber library for front end tests with Jest

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!