This repository illustrates how to use Mock Service Worker to mock a REST API for development, unit and E2E testing in Create React App project.
- Create React App
- Jest for running unit tests;
- React Testing Library for unit test assertions;
- Cypress for running E2E tests;
$ git clone https://github.com/mswjs/examples.git
$ cd examples
$ yarn
$ cd rest-react
$ yarn start
$ yarn test:unit
$ yarn test:e2e
src/mocks/handlers.js
describes request handlers to use.
src/mocks/browser.js
sets up the Service Worker.src/index.js
conditionally enables mocking indevelopment
environment.public/mockServiceWorker.js
the Service Worker, created by runningnpx msw init public
.
src/mocks/server.js
sets up the "server" to use the same mocking logic in Node.src/setupTests.js
enables mocking for unit tests viabeforeAll
/afterAll
hooks.