-
-
Notifications
You must be signed in to change notification settings - Fork 273
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
Allow overriding entire handler function in generated MSW handlers, not just response object #1206
Comments
Thank you for made this issue.
Regarding alternatives, when extending and defining a mock definition in a test, for example, you can use the following function that automatically generates the key.
|
Thanks for your reply! The
Overall, I'm afraid this would not be a good developer experience. So for now, my team will keep hard-coding URLs in tests. And I would be delighted if in the future, Orval will provide support for more flexible handler overrides. |
Yes, this is an alternative method, but it is consistent and functional with the URL defined in OpenAPI, so please consider it as one method.
|
If you want to test different response types, you can use the output: {
mock: {
type: "msw",
generateEachHttpStatus: true
}
} With a specification that have two responses of a export const getListPetsMockHandler200 = (
overrideResponse?:
| Pets
| ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Pets),
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);
return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
? typeof overrideResponse === 'function'
? overrideResponse(info)
: overrideResponse
: getListPetsResponseMock200(),
),
{
status: 200,
headers: {
'Content-Type': 'application/json',
},
},
);
});
};
export const getListPetsMockHandler500 = (
overrideResponse?:
| Error
| ((info: Parameters<Parameters<typeof http.get>[1]>[0]) => Error),
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);
return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
? typeof overrideResponse === 'function'
? overrideResponse(info)
: overrideResponse
: getListPetsResponseMock500(),
),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
});
}; And you no longer need to overwrite the whole mock handler. |
This is a feature request, not a bug.
What are the steps to reproduce this issue?
#1186 made it possible to override response objects in MSW handlers. Thank you for that! Unfortunately, we often cannot use them in our project in many tests because we're missing two things:
Here's an example of a handler generated by Orval:
Simulating HTTP errors in tests
Then, for example, a unit test needs to simulate a HTTP 500 error. It currently cannot use
getProjectQueryMockHandler
for that since that always uses HTTP 200. So we instead don't use any code generated by Orval, and write something like this. Unfortunately, this is a lot less type-safe, since there's nothing checking that the pathprojects/:projectId
is correct.What we'd love to see is
getProjectQueryMockHandler
allowing callers to either override the response, or the entire handler function. Something like:Alternative solution: Generate a function to get the path of the endpoint, such as:
Changing request based on the response
Similarly, we have tests where the MSW handlers need to return a response that depends on what is in the request. So we currently write something like:
Ideally, we could write this as:
Any other comments?
Allowing overriding of the entire handler function is also what @graphql-codegen/typescript-msw offers, which we use for our GraphQL endpoints.
Still, orval is great! I expect it to be a significant boost in DevEx for our team.
What versions are you using?
Package Version: 6.24.0
The text was updated successfully, but these errors were encountered: