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

191
Views
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 answers
Answer question

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 Report
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!