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

193
Visualizações
Mock external library call using jest

New to jest and react code testing. I am testing a file which imports base64 from react-native-base64 and decodes a value that is fetched from backend system in useEffect. Below is the sample code.

import base64 from "react-native-base64";
 
  const xyzWork = async () => {
    const value = await - fetch data from backend
    const decodedValue = base64.decode(value);
    ...
    ...
    ...
  }

  useEffect(() => {
    xyzWork();
  }, []);


  return <div><SomeComponent /></div>;

I am facing difficulties while testing this code using jest. I tried multiple ways to test the same but it is failing with multiple errors.

Some of the ways, import base64 same way and mock like this. base64.decode = jest.fn(()=> "testParsedValue");

Tried to mock entire library itself like const mockedLib = jest.mock("react-native-base64", () => jest.fn()); and then mock base64 of that variable but nothing seems working.

Any help would be appreciated! Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try mocking it using the Jest module factory e.g.

jest.mock('react-native-base64', () => ({
    decode: () => "testParsedValue"
}));
about 4 years ago · Juan Pablo Isaza Relatório
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