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

178
Views
Jest mock export exposing axios create instance

This is easy but I have been fighting for 5hrs and I don't have a solution yet

app.ts
import { apiClient, getCustomFieldMapping } from './apiClient'

const otherFunction = () => {
   // just to show that getCustomFieldMapping it's used
   getCustomFieldMapping()
}


// route /ext/:userId
const callApi = async (path: string,) => {
     const url = apiClient.defaults.baseURL + path
     return apiClient.get(url);
}
apiClient.ts

import axios from "axios";

export const apiClient = axios.create({
  baseURL: "http://localhost:3000",
  auth: {
    username: `${config.zendeskUsername}/token`,
    password: config.zendeskToken,
  },
});

export const getCustomFieldMapping = (
  from: "fieldId" | "name",
  value: string
): TicketFieldMapping => {
  const fieldMap = fieldMappings.find((mapping) => mapping[from] === value);
  if (!fieldMap) {
    throw {
      message: `Zendesk custom field's mapping from "${from}" "${value}" does not exists.`,
    };
  }
  return fieldMap;
};

What is actually happening is that i'm not been able to only mock the GET api call from the apiClient module.

I have tried a lot of things (listed after this) like:

test.ts

// first try
import axios from 'axios'
jest.mock("axios", () => ({
  create: jest.fn(),
}));
  test("Get user", async () => {
    (axios.create as jest.Mock).mockImplementation(() => {
      return {
        defaults: {
          baseURL: "https://localhost:3000",
        },
        get: jest
          .fn()
          .mockResolvedValueOnce({})
      };
    });
    return request(app)
      .get("/ext/121")
      .expect(StatusCodes.NOT_FOUND)
      .then((response) => {
        console.log(response.body);
      });
  });

// I get that apiClient is undefined

// another one is
const mockGet = jest.fn();
jest.mock("./apiClient", () => {
  const actual = jest.requireActual("./apiClient");
  actual.apiClient.get = jest.fn().mockImplementation(() => mockGet);
  return actual;
});

test("Get user", async () => {
    mockGet.returnResolvedValueOnce({})
    return request(app)
      .get("/ext/121")
      .expect(StatusCodes.NOT_FOUND)
      .then((response) => {
        console.log(response.body);
      });
  });


// here I have two things.. one is that defualts is undefiend and it breaks the run

// otherone it's that getCustomFieldMapping is undefined and can't be called

I want to mock the apiClient GET function. that's it

Thanks in advance

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!