This repo exists as an example of using Mock Service Worker (MSW) to improve unit tests of components that make API calls to fulfill their purpose.
Check out the master branch to see the example making use of MSW to mock the API at the network layer.
Check out the traditional-mocking branch to see the example using traditional mocking techniques to mock the API at the service layer.
In a terminal enter npm t to start a Jest test run.
A working API has been provided using the json-server package to experience the application running against a functioning back end.
In a terminal enter npm run api to start the sample server.
In a terminal enter npm start to launch the msw-demo application. If the sample api is not running the app will render an error message. If the sample API has been started a list of three tasks matching those found in ./src/api/db.json will be returned by the json-server api and rendered to the screen.
In a terminal enter npm run start:msw to launch the msw-demo application with MSW mocking enabled. The app will render a list of two tasks corresponding to the mockTasks found in ./src/api-mocks/handlers.