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

110
Views
Unsure of how much to mock with jest.js in a function that is primarily made up of API calls?

An outline of my code is this:

const apiOnePost = async (data) => {
    console.log('posting the data');
};

const apiTwoPost = async (data) => {
    console.log('posting the data');
    return 'id';
};

const apiTwoGet = async (id) => {
    console.log('getting the data');
    return { rawData: 'foo' }
}

// Allready united tested
const mapData = (raw) => {
    return { data: raw.rawData }
}

const businessFunction = async (data) => {
    await apiOnePost(data);
    let id = await apiTwoPost(data)
    let raw = apiTwoGet(id);
    return mapData(raw);
}

My objective is to test the function businessFunction (which is the only function that is exported from the file, mapData is actually in a separate file)

But as you see I can't just import businessFunction into my test file and run asserts because all it's doing is making api calls and mapping the results.

My test is strictly not supposed to send data to and from the APIs.

If I mock the apis how do I get the function to use those mocked apis instead of the real things when I'm running it to test my assertions?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If anyone is curious the solution I ended up choosing was separating each API post into it's own module and importing them in the main function, then running assertions on the main function was trivial because I could use jest.mock() on each API call.

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!