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

Provide mock REST API endpoint #6

Open
angeloashmore opened this issue Oct 14, 2021 · 0 comments
Open

Provide mock REST API endpoint #6

angeloashmore opened this issue Oct 14, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@angeloashmore
Copy link
Member

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:

import * as prismicM from "@prismicio/mock";
import * as mswNode from "msw/node";

const server = mswNode.setupServer();

const queryResponse = prismicM.api.query({
	documents: [
		prismicM.value.document(),
		prismicM.value.document(),
	],
});

server.use(prismicM.msw.queryHandler({ queryResponse }));

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.

@angeloashmore angeloashmore added the enhancement New feature or request label Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant