Skip to content

Latest commit

 

History

History
55 lines (36 loc) · 1.55 KB

README.md

File metadata and controls

55 lines (36 loc) · 1.55 KB

Create React App (GraphQL)

This repository illustrates how to use Mock Service Worker to mock a GraphQL API for development, unit and E2E testing in Create React App project.

Technologies

Getting started

$ git clone https://github.com/mswjs/examples.git
$ cd examples
$ yarn
$ cd graphql-react-apollo

Running locally

$ yarn start

Tests

Unit tests

$ yarn test:unit

E2E tests

$ yarn test:e2e

Key points

Browser

  • src/index.js conditionally enables mocking in development environment.
  • src/ApolloClient.js uses an explicit fetch reference in order for requests to be captured and deferred until the Service Worker is ready. Necessary due to Apollo hoisting a native window.fetch call, preventing Mock Service Worker from properly capturing it.
  • public/mockServiceWorker.js the Service Worker, created by running npx msw init public.

NodeJS