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

Suggests similar request handlers upon unhandled requests #560

Merged
merged 6 commits into from
Feb 2, 2021

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jan 22, 2021

GitHub

Changes

  • Removes a "dummy" request handler code snippet from the unhandled request message. The library cannot reliably guess what kind of API you attempt to mock. The safest option is to link to the documentation where the developer decides on the right approach for them.
  • Suggests a similar request handler based on the following criteria:
    • Similar request handler URL (REST API). Request handlers with the same method are always treated as more similar. Request handlers with a different method can still be suggested if there isn't any similar request handler of the same method.
    • Similar request handler operation name (GraphQL API). The same as with REST API handlers, GraphQL handlers are treated as more similar if their operation type matches the original request's operation type. Handlers of a different operation type can still be suggested if their operation name is similar.

Roadmap

  • Provide GraphQL suggestions.
  • REST: Suggest a handler of a different method if its path is similar.
  • GraphQL: Suggest a handler of a different operation type if its name is similar.
  • GraphQL: Ensure that handler suggestions are presented by the operation type. If there are multiple similar handlers, suggest the one with the same operation type as the request first.
  • Suggests multiple similar handlers to increase the usefulness of the suggestions.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 22, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9d0addc:

Sandbox Source
MSW React Configuration

@kettanaito kettanaito marked this pull request as draft January 22, 2021 16:01
@kettanaito kettanaito added the DX label Jan 28, 2021
@kettanaito kettanaito force-pushed the 386-dx-on-mock-fail branch 2 times, most recently from 1ec77be to 76551dd Compare January 28, 2021 11:37
@kettanaito kettanaito marked this pull request as ready for review January 28, 2021 11:39
@kettanaito
Copy link
Member Author

Although graphql.parse() and parseGraphQLRequest have a similar logic, we cannot fully replace the former with the latter. The parsing logic of a GraphQL handler is far more complex: it deals with variables, the building of a multi-part/multi-operation body. We will simplify the parsing state as a part of Request Handler API refactoring (#561).

@kettanaito kettanaito changed the title Improves DX when dealing with unhandled requests Suggests similar request handlers upon unhandled requests Jan 28, 2021
Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a beauty @kettanaito !
I left a couple of subjective notes to make it a bit more readable.

src/utils/request/onUnhandledRequest.ts Outdated Show resolved Hide resolved
src/utils/request/onUnhandledRequest.ts Outdated Show resolved Hide resolved
src/utils/request/onUnhandledRequest.ts Outdated Show resolved Hide resolved
@kettanaito kettanaito force-pushed the 386-dx-on-mock-fail branch 3 times, most recently from 152a80b to 21e03ab Compare February 1, 2021 15:56
@kettanaito
Copy link
Member Author

@timdeschryver has made a great suggestion to include multiple handlers in the suggestions list. The first similar handler may not be the one a developer would be looking for. Including a list of up to 3-5 items of the suggestions with the same matching indices would be quite sensible.

We can use worker.use() to provision per-test overrides to account for multiple possible combinations without polluting the same *.mocks.ts definition file.

@kettanaito kettanaito merged commit d7834c1 into master Feb 2, 2021
@kettanaito kettanaito deleted the 386-dx-on-mock-fail branch February 2, 2021 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants