Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda