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

Duplicate mock and MSW handler for status 200 when using generateEachHttpStatus=true #1452

Closed
severinh opened this issue Jun 11, 2024 · 2 comments
Labels
bug Something isn't working mock Related to mock generation
Milestone

Comments

@severinh
Copy link
Contributor

severinh commented Jun 11, 2024

What are the steps to reproduce this issue?

Use an Orval configuration with client: 'react-query' and mock: { type: 'msw', generateEachHttpStatus: true, ... }

What happens?

Orval generates two mock functions and two mock handler functions that are completely identical:

export const getProjectInsightsQueryResponseMock = (): InsightsResponse => ({
  // A very long mock definition
});

export const getProjectInsightsQueryResponseMock200 = (): InsightsResponse => ({
  // Exactly the same mock definition, again
});

export const getProjectInsightsQueryMockHandler = (
  overrideResponse?:
    | InsightsResponse
    | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<InsightsResponse> | InsightsResponse)
) => {
  return http.get('*/data/analytics/projects/:projectId/insights', async (info) => {
    return new HttpResponse(
      JSON.stringify(
        overrideResponse !== undefined
          ? typeof overrideResponse === 'function'
            ? await overrideResponse(info)
            : overrideResponse
          : getProjectInsightsQueryResponseMock()
      ),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        },
      }
    );
  });
};

export const getProjectInsightsQueryMockHandler200 = (
  overrideResponse?:
    | InsightsResponse
    | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<InsightsResponse> | InsightsResponse)
) => {
  // Duplicate of getProjectInsightsQueryMockHandler
  return http.get('*/data/analytics/projects/:projectId/insights', async (info) => {
    return new HttpResponse(
      JSON.stringify(
        overrideResponse !== undefined
          ? typeof overrideResponse === 'function'
            ? await overrideResponse(info)
            : overrideResponse
          : getProjectInsightsQueryResponseMock200()
      ),
      {
        status: 200,
        headers: {
          'Content-Type': 'application/json',
        },
      }
    );
  });
};

What were you expecting to happen?

This works, but the duplication unnecessarily bloats the generated MSW file.

IMO the ideal output would be for getProjectInsightsQueryResponseMock to just be an alias of getProjectInsightsQueryResponseMock200, and same for getProjectInsightsQueryMockHandler.

For example:

export const getProjectInsightsQueryResponseMock200 = (): InsightsResponse => ({
  // Long mock definition
});

export const getProjectInsightsQueryResponseMock = getProjectInsightsQueryResponseMock200;

export const getProjectInsightsQueryMockHandler200 = (
  overrideResponse?:
    | InsightsResponse
    | ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Promise<InsightsResponse> | InsightsResponse)
) => {
  // Omitted
};

export const getProjectInsightsQueryMockHandler = getProjectInsightsQueryResponseMock200;

Any logs, error output, etc?

N/A

Any other comments?

This is not high priority request. Functionally, generateEachHttpStatus=true works fine.

Though I believe there is benefit in the output of orval being clean and easy to read, since that contributes towards a positive DevEx.

What versions are you using?

Operating System: Does not matter for the issue
Package Version: 0.30.2
Browser Version: Does not matter for the issue

@melloware
Copy link
Collaborator

@TommoLeedsy can you verify this is fixed with your latest change: #1445

@melloware melloware added the bug Something isn't working label Jun 11, 2024
@melloware melloware added this to the 6.31.0 milestone Jun 11, 2024
@melloware melloware added the mock Related to mock generation label Jun 11, 2024
@melloware
Copy link
Collaborator

@severinh 6.31.0 i released can you test this and confirm whether its fixed or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mock Related to mock generation
Projects
None yet
Development

No branches or pull requests

2 participants