Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Axios client Testing #1199

Open
jezzsantos opened this issue Oct 27, 2024 · 3 comments
Open

Example Axios client Testing #1199

jezzsantos opened this issue Oct 27, 2024 · 3 comments
Labels
docs 📃 Improvements or additions to documentation internal ⚙️ Internal development related

Comments

@jezzsantos
Copy link

Description

Please can you provide an example of testing some code that uses one of the generated services, when using the axios client?

Ideally, showing how to mock axios under the covers (i.e. using Jest)

@jezzsantos jezzsantos added the feature 🚀 New feature or request label Oct 27, 2024
@jezzsantos
Copy link
Author

jezzsantos commented Oct 27, 2024

I guess I am trying to find out how to mock axios so that I am not actually issuing calls across the internet, and I can simply make sure the right request is being made, and verify it was made.

So, it looks like it comes down how to mock axios so that when the client is used, I can fake the call in axios:
This is what I have so far, but I cannot seem to find which methods on axios to mock correctly.

I have this so far, but the client seems to use different methods, so this does not work.
I am unable to debug into the axios-client library to figure out how it works, and what to mock,
(I am guessing here, but it doe snot work)

jest.mock("axios", () => {
  const axiosImpl = jest.requireActual("axios");

  return {
    ...axiosImpl,
    create: jest.fn().mockImplementation((config) => {
      const createImpl = axiosImpl.create(config);
      return {
        defaults: createImpl.defaults,
        request: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
        interceptors: createImpl.interceptors,
        get: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
        post: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
        put: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
        delete: jest.fn(() => Promise.resolve({ data: {}, status: 200 }))
      };
    }),
    request: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
    get: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
    post: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
    put: jest.fn(() => Promise.resolve({ data: {}, status: 200 })),
    delete: jest.fn(() => Promise.resolve({ data: {}, status: 200 }))
  };
});

@jezzsantos
Copy link
Author

Please can we see an example of how to unit test (i.e. with Jest) how to verify a call was made to one of the generated services?

@mrlubos mrlubos added docs 📃 Improvements or additions to documentation internal ⚙️ Internal development related and removed feature 🚀 New feature or request labels Oct 27, 2024
@mrlubos
Copy link
Member

mrlubos commented Oct 27, 2024

Hi @jezzsantos, I've added this to the documentation/examples backlog, but unlikely to be addressed any time soon. You might also try your luck in https://github.com/orgs/hey-api/discussions, though I am not sure if anyone will give you a reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs 📃 Improvements or additions to documentation internal ⚙️ Internal development related
Projects
None yet
Development

No branches or pull requests

2 participants