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

412
Views
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
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!