0

I am attaching all the function snippets below. I am using jest to run a unit test on this function but this needs to mock axios. I tried like this :

// TODO - mock axios class instance for skipped Test suites
describe("dateFilters()", () => {
    beforeEach(() => {
        jest.resetAllMocks();
    });
    it("Mock Fetch API for Date Options Response", async () => {
        const mockFn = jest.fn();
        setUpMockResponse(mockFn, mockFetchDateOptionsResponse);
        const response = await dateFilters(Workload.WIN32);
        expect(mockFn).toHaveBeenCalledTimes(1);

        expect(response?.data).toEqual(mockFetchDateOptionsResponse);
    });
});

The error I am getting is : thrown: "Exceeded timeout of 5000 ms for a test. Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test." It seems it is not mocking anything.

All the require function definitons are below:

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


export const getKustoResponse = async ({
    queryName,
    platform,
    queryParams,
    cluster = "Default",
}: QueryDetail) => {
    const dbName = getClusterValue({ platform, cluster, key: "db" });
    const url = getClusterValue({ platform, cluster, key: "kustoUrl" });
    const postBody = {
        db: dbName,
        csl: queryParams
            ? substituteQueryParameters(queries[queryName], queryParams)
            : queries[queryName],
    };

    const apiClient = ApiClient.getInstance();
    const response = await apiClient.post(url, postBody, {
        headers: {
            ...kustoApiRequestDefaultConfiguration.headers,
            "x-ms-kql-queryName": queryName,
        },
        timeout: kustoApiRequestDefaultConfiguration.timeout,
    });
    return response;
};

import Axios, { AxiosInstance } from "axios";
import axiosRetry from "axios-retry";
export class ApiClient {
    private static instance: AxiosInstance;

    public static getInstance = (): AxiosInstance => {
        if (!ApiClient.instance) {
            ApiClient.createInstance();
        }

        return ApiClient.instance;
    };

    private constructor() {
        ApiClient.getInstance();
    }

    protected static createInstance = () => {
        const responseType = "json";
        const client = Axios.create({
            responseType,
        });
        axiosRetry(client, apiRetryConfiguration);
        client.interceptors.request.use(requestInterceptor);
        client.interceptors.response.use(responseInterceptor, errorInterceptor);

        ApiClient.instance = client;
    };
}


export const requestInterceptor = async (
    request: AxiosRequestConfig
): Promise<AxiosRequestConfig> => {
    const token = await getKustoToken();
    request.headers = { ...request.headers, Authorization: `Bearer ${token}` };
    return request;
};
etotientz
  • 373
  • 5
  • 18

1 Answers1

1

There is no fetch call in your source code. Is it in the apiClient? If so, do this:

jest.spyOn(apiClient, 'post').mockImplementation();
expect(apiClient.post).toHaveBeenCalled();