You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
When writing tests involving @prismicio/client or the Prismic REST API directly, @prismicio/mock can be used to mock return values. Currently this can be done by manually setting up a mock API within tests, such as through a custom request handler via Mock Service Worker (MSW).
Setting this up per-project can be a hassle as it requires intricate URL parameter and authentication checks.
Describe the solution you'd like
A test-specific mock API solution could be provided by @prismicio/mock. There are many ways to do this, such as building a full HTTP server or providing a mocked fetch function.
Rather than build a completely custom solution, it makes sense to integrate with an existing HTTP mocking solution. Mock Service Worker, as mentioned above, is a common library made specifically for this purpose.
@prismicio/mock could export MSW REST API handlers that cover most mocking cases. Its purpose is not to reimplement the Prismic REST API. Instead, it is just the bridge between an HTTP request and a set of provided results.
Preparing an API handler with MSW could look something like this:
It uses the existing mock generators which allows users to fine-tune the results.
To restate, the provided handlers do not re-implement the REST API. A request with special parameters, such as predicates or fetchLinks, is not processed by the handler. Instead, the user must prepare the response manually.
The handler can, however, validate against the URL to ensure the correct URL was requested.
Describe alternatives you've considered
The following libraries implement API mocking manually using MSW and @prismicio/mock (or @prismicio/types with manual mocks):
Rather than integrating with a specific language and framework, it exposes an API endpoint that returns static mock data. This makes it compatible with any solution, provided test requests are routed to the local URL.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
When writing tests involving
@prismicio/client
or the Prismic REST API directly,@prismicio/mock
can be used to mock return values. Currently this can be done by manually setting up a mock API within tests, such as through a custom request handler via Mock Service Worker (MSW).Setting this up per-project can be a hassle as it requires intricate URL parameter and authentication checks.
Describe the solution you'd like
A test-specific mock API solution could be provided by
@prismicio/mock
. There are many ways to do this, such as building a full HTTP server or providing a mockedfetch
function.Rather than build a completely custom solution, it makes sense to integrate with an existing HTTP mocking solution. Mock Service Worker, as mentioned above, is a common library made specifically for this purpose.
@prismicio/mock
could export MSW REST API handlers that cover most mocking cases. Its purpose is not to reimplement the Prismic REST API. Instead, it is just the bridge between an HTTP request and a set of provided results.Preparing an API handler with MSW could look something like this:
It uses the existing mock generators which allows users to fine-tune the results.
To restate, the provided handlers do not re-implement the REST API. A request with special parameters, such as predicates or
fetchLinks
, is not processed by the handler. Instead, the user must prepare the response manually.The handler can, however, validate against the URL to ensure the correct URL was requested.
Describe alternatives you've considered
The following libraries implement API mocking manually using MSW and
@prismicio/mock
(or@prismicio/types
with manual mocks):@prismicio/client
@prismicio/react
gatsby-source-prismic
gatsby-plugin-prismic-previews
Additional context
Stripe provides a similar solution via a Docker container: https://github.com/stripe/stripe-mock
Rather than integrating with a specific language and framework, it exposes an API endpoint that returns static mock data. This makes it compatible with any solution, provided test requests are routed to the local URL.
The text was updated successfully, but these errors were encountered: