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

255
Views
Unable to mock axios call in Jest

It seems mocking is not working for me via axios as it seems to make acrtual API calls ( visible because I am getting 401 status code when running jest test ) I am not sure why am I not able to mock axios.Can anyone point out the mistake I am making?

index.test.ts

describe("compositeScore()", () => {

    it("Mock Fetch API for Composite Score Response", async () => {
        
        const mock = jest.spyOn(axios, "post");
        mock.mockReturnValueOnce(mockResponse);
        const response = await dateFilter(platform);
        expect(mock).toHaveBeenCalledTimes(1);
        expect(response).toEqual(mockFetchCompositeScoreResponse);
    });
});

index.ts

export const dateFilters = async (platform) => {
    const dates = await fetchWrapper(
        platform.toLowerCase().concat("DateFilters"),
        platform,
        {}
    );
    return dates;
};


export async function fetchWrapper(
    queryName: string,
    platform: string,
    queryParams?: {}
) {
   
    const headers = {
        Accept: "application/json",
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json",
     
    };
    const config: AxiosRequestConfig = {
        method: "post",
        url,
        headers,
        data: {
            db: dbName,
            csl: queryParams
                ? substituteQueryParameters(queries[queryName], queryParams)
                : queries[queryName],
        },
    };

    return axios(config);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try

import axios from 'axios';

jest.mock('axios');

You'll need to mock the module first before you import in the System Under Test.

https://jestjs.io/docs/mock-functions#mocking-modules

about 4 years ago · Juan Pablo Isaza Report
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!