Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda