-
-
Notifications
You must be signed in to change notification settings - Fork 336
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
[Proposal] possible to pass the intercept function to the msw
mock handler
#1668
Comments
Hi, @melloware |
Sounds reasonable to me |
Thanks for taking the time to write the proposal, @soartec-lab! I'd like to better understand what you'd like to achieve in the example you give. Couldn't you already achieve what you describe with the current API of orval? getGetPetMockHandler((info) => {
// You can do anything you want here, such as asserting that the API is called.
return { message: 'hello'};
}); What worries me (as you already point out) is that the proposal would be a breaking change that will require developers who use orval to change many/all of their tests. And simple tests (without intercept) would require more boilerplate, such as |
Let me introduce you to that thinking. |
Thanks @soartec-lab. I think there's a misunderstanding here. When you pass a function to getGetPetMockHandler((info) => {
// This will be called each time a request to the API is made. You can anything you want to do here, such as making assertions.
// You can for example even access the concrete request that the application made, via `info.request`.
return { message: 'hello'};
}); |
Exactly. I certainly misunderstood. But there is a further problem. getGetPetMockHandler(
{
overrideResponse: { message: 'hello'},
overrideStatus: 500,
overrideHeader: { 'Content-Type': 'application/pdf' },
intercept: (info: any) => {
mockFn(info)
},
}
) |
@soartec-lab You're right that being able to override more aspects of the However, I don't think passing an object with all kinds of To give developers more control over the MSW getGetPetMockHandler((info) => {
// if the developer wants, they can do anything here, such as inspect info.request
return HttpResponse.json({ message: 'hello'}, { headers: 500, contentType: ... });
}); That would not require a complex new API - you'd just give Orval developers the option access the MSW API, if they want to. Btw, this is also the approach taken by other MSW integrations (such as for GraphQL). Most importantly, Orval should keep the option to just use the existing, simple, very compact What do you think? I hope that's understandable. Happy to provide more details, but I'll be off the grid for the next 3 weeks. |
Thank you for your reply. getGetPetMockHandler((info) => {
// if the developer wants, they can do anything here, such as inspect info.request
return HttpResponse.json({ message: 'hello'}, { headers: 500, contentType: ... });
}); |
What are the steps to reproduce this issue?
This issue is new proposal
What happens?
This makes it easy to check whether this API is being called correctly in a test, for example.
Currently, to achieve this, it is not possible to use generated mocks, and necessary to implement them in tests each time.
By doing this, it becomes possible to intercept function execution, making testing easier.
What were you expecting to happen?
I want to use it like below:
Any logs, error output, etc?
none.
Any other comments?
If i make this change, it will be a breaking change.
What versions are you using?
Please execute
npx envinfo --system --npmPackages orval,zod,axios,msw,swr,@tanstack/react-query,@tanstack/vue-query,react,vue
and paste the results here.The text was updated successfully, but these errors were encountered: