This repository illustrates how to use Mock Service Worker to mock a REST API for development, unit and E2E testing in an Angular project.
- Jest for running unit tests;
- Angular Testing Library for unit test assertions;
- Cypress for running E2E tests;
- Cypress Testing Library for end to end test assertions;
$ git clone https://github.com/mswjs/examples.git
$ cd examples
$ yarn
$ cd rest-angular$ yarn start$ yarn test:unit$ yarn test:e2esrc/mocks/handlers.jsdescribes request handlers to use.
src/mocks/browser.tssets up the Service Worker.src/main.tsconditionally enables mocking indevelopmentenvironment.src/mockServiceWorker.jsthe Service Worker, created by runningnpx msw init src.- Add
src/mockServiceWorker.jsfile to theassetslist in yourangular.jsonfile.
src/mocks/server.tssets up the "server" to use the same mocking logic in Node.src/setup-jest.tsenables mocking for unit tests viabeforeAll/afterAllhooks.