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

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