mswjs / examples

Examples of Mock Service Worker usage with various frameworks and libraries.
683 stars 211 forks source link

There is no condition inclusion in main.ts #76

Closed rantiev closed 2 years ago

rantiev commented 2 years ago

docs says -> src/main.ts conditionally enables mocking in development environment.

But where is it there?

kettanaito commented 2 years ago

Hi, @rantiev.

In our Angular example, we enable Mock Service Worker as a part of the application's environment. Here you can see what the worker is imported and started in the development environment:

https://github.com/mswjs/examples/blob/2338457a82f60e5d76a9a32a742157af21bf3606/examples/rest-angular/src/environments/environment.ts#L5-L6

I believe Angular loads this environment by default so you don't have to explicitly state that. Then, in the entry module, we enable the production environment conditionally:

https://github.com/mswjs/examples/blob/2338457a82f60e5d76a9a32a742157af21bf3606/examples/rest-angular/src/main.ts#L7-L9

The production environment does not import or start the worker, so there's no API mocking in production.

This is a setup specific to Angular and has been prepared for developers working with Angular much closer than I ever have. I think it makes sense but if you find it confusing feel free to propose how we would improve it. Thanks.

kettanaito commented 2 years ago

To begin with, I think we can update the README.md to explain that we couple mocking with the environment. @rantiev, would you be interested in opening a pull request with that change?