-
-
Notifications
You must be signed in to change notification settings - Fork 613
Description
Description
Hello,
in our project at work, we heavily rely on openapi-ts and OpenAPI-Fetch in our communication between our frontend and backend. We love the idea behind this library and are really happy.
The only pain point we initially had was mocking calls to and responses from our backend inside our frontent test. We wanted to have a similarly typed mocking API that is descriptive, helps us with autocompletion, ensures that our mocked responses match the specification and also validates that the endpoint was called with the correct and expected parameters (be it path, query, header, ...).
We decided on the library https://github.com/wheresrhys/fetch-mock. In the tests, it replaces the actual fetch method with a fake implementation that checks against configured routes and returns predefined responses. It has extensive matching logic to distinguish two calls based on their request content and an intuitive API. To also have typing support when using fetch-mock in our tests, we have written a small wrapper to only allow mocking of endpoints that conform with our OpenAPI spec. Our current implementation is quite crude and does not include the full API of fetch-mock and also not all features of OpenAPI, but has proven essential in writing and maintaining mock definitions in our tests with changing OpenAPI definitions.
I think what we created would benefit the community as well, and I would really like to publish the code I have written to the open source community. I plan to refactor and generalize the typing code to include both the complete fetch-mock API, as well as support all OpenAPI features. In essence, I wan't to write a wrapper package similar to openapi-fetch, but for the fetch-mock library.
Proposal
My proposal would be a further package in this repository called 'openapi-fetch-mock' containing a wrapper for the fetch-mock library. Its implementation is heavily inspired by the 'openapi-fetch' package
The goal of this new library would be to allow the user to declare mocked requests, define expected request parameters, and validate for captured fetch calls using an API that is fully typed with the types provided by openapi-typescript. I have already openend an issue in the fetch-mock repository talking about my idea with the creator of this library: wheresrhys/fetch-mock#962. It also contains a more comprehensive version of my ideas.
I have already created a fork and started playing around with ideas: https://github.com/noahzuch/openapi-typescript. There exist some initial tests, where the functionality can be seen.
What do you think? Is this something that could be added to this repository? I am happy to contribute and open for suggestions.
Extra
- I’m willing to open a PR (see CONTRIBUTING.md)