Mock axios requests done via axios-digest-auth

Viewed 16

I'm using this npm module: https://github.com/mhoc/axios-digest-auth to perform axios requests as it takes care of the digest auth process.

I'm trying to create some unit tests and I need to mock the axios requests to do so. Usually axios-mock-adapter works just fine for me when I am sending requests directly with axios, but it does not seem to work when doing the requests via axios-digest-auth.

This is the function I'm trying to unit-test:

async getOrganization(orgId: string): Promise<Organization> {
   const response = await this.axiosDigestAuth.request({
     headers: { Accept: "application/json" },
     method: "GET",
     url: `https://api.com/getOrg/${orgId}`,
   });
   
   // If I use this, the mock works

   // const response = await axios.get(uri, {
   //   headers: { Accept: "application/json" },
   // });

   return response.data as Organization;
}

and this is the test:

import axios from "axios";
import AxiosMockAdapter from "axios-mock-adapter";

it("Returns organization data by id", async () => {
    const orgId = "organization-id";
    axiosMock = new AxiosMockAdapter(axios);

    axiosMock.onGet(`https://api.com/getOrg/${orgId}`).reply(200);

    try {
      const result = await getOrganization(orgId);
    } catch (error) {
      logAxiosError(error);
    }
});

Any idea why the mock would not work if axios-digest-auth performs an axios request under the hood?

Any suggestions on how to properly mock it?

Thanks

0 Answers
Related