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

106
Visualizações
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 Respostas
Responde à pergunta

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