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

414
Visualizações
Mocking a nested function in Jest

How do I mock/spyOn the value of a function returned after calling another function? In the following example I would like to mock the resolved value for the 'get' method of api.get() but I've been having difficulty getting at that value with a jest mock or spyOn. The issue is that the get method is returned after the function is called as part of axiosInstanceWrapper({}).

axios-instance-wrapper.js:

...

const axiosInstanceWrapper = function(params) {
  ...
  const instance = axios.create(otherInstanceParams);
  ...
  return instance;
}

module.exports = axiosInstanceWrapper;

code:

import axiosInstanceWrapper from './axios-instance-wrapper';

const api = axiosInstanceWrapper({});
const result = api.get(MY_URL, myParams); <-- I want to mock this response

test file:

import axiosInstanceWrapper from './axios-instance-wrapper';


jest.mock('./axios-instance-wrapper', () => {
  return () => ({
    default: jest.fn().mockReturnValue({
      get: jest.fn()
    })
  });
});

...

test('', () => {
 const spy = jest.spyOn(axiosInstanceWrapper, 'get').mockResolvedValue({
      data: {
        customer: {
          subscriptionContracts: [],
          pageInfo: {
            hasNextPage: false,
            hasPreviousPage: false,
          }
        }
      }
    });
});

Error I get on that line:

Error: Cannot spy the get property because it is not a function; undefined given instead

Other things I've tried:

const spy = jest.spyOn(axiosInstanceWrapper.default, 'get').mockResolvedValue({
  ...
});
about 4 years ago · Juan Pablo Isaza
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