Open kettanaito opened 3 years ago
I strongly support this 👍
Wholeheartedly support this idea 👍
In fact, MSW is built right into my team's template (which is based on CRA): https://github.com/PublicisSapient/cra-template-accelerate. Making it an integral part of CRA will give greater visibility to this awesome tool, and will help React developers improve the quality of their work.
I've spent numerous hours mocking and stubbing data for tests over the years wishing for a better approach and tool. I was recently introduced to MSW and love how it instantly improves the developer experience. Would love to see it as a default in CRA.
Good Idea 👍 BTW anyone know is there tool kind of convert MSW mock-server to real backend server like Express?
@ryota-murakami, as a matter of fact, we've just released an extension that does just that. It's called http-middleware and you can read about it in this post.
@kettanaito this is awesome! That's exactly what I've been looking for!
@kettanaito Hello Artem! I'm created opinionated configured Vite extended template as a CRA alternative.
https://github.com/laststance/vite-react-ts-extended
It's installed msw already but currently I had only setup for jest.
I'd like to add browser side setup as a built-in data fetch/mutation solution, but I'm not familiar msw as a service worker.
I wanna ask you your prefer setup as a author, I followed getting start document though.
Or send PullRequest directory is really really appropriate it if you have a little free time! 😀
Main use cases are Demo, Prototyping, little experience any library or React itself etc.
Is your proposal related to a problem?
Indirectly.
Create React App comes with React Testing Library as a great default for testing React applications. Elaborating on the topic of great defaults, I think it's safe to say that most CRA projects involve some extent of data fetching. Those components that do HTTP requests must be tested as well, so developers have full confidence in the applications they build. I think it makes sense to include an API mocking layer in CRA with a minimal setup that not only achieved the goal, but encourages best practices along the way.
Describe the solution you'd like
I'd like to suggest integrating Mock Service Worker (MSW) as the default API mocking solution in Create React App.
You can see the example of the proposed integration that features the minimalistic setup and re-usage of the same mock definitions in integration and E2E tests. I will be happy to open a pull request that showcases the changes necessary to have the library set up in Create React App.
Why I think Mock Service Worker is a good suggestion
window.fetch
, React Query, Apollo, SWR, they will be able to capture and mock the responses they need.MockedProvider
in Apollo.Describe alternatives you've considered
There are no alternatives (to my best knowledge) that would allow seamlessly reusing the same mock definitions across environments and, thus, different testing levels, on the same scale that MSW does. The current testing reality is that developers configure different tools, often repeating the testing setup, to achieve the same goal—all that to compensate for how such tools are written and function. Code repetition is the least concern here, as the technical implementation of most API mocking tools replace request-issuing clients, significantly decreasing the confidence in any related tests, as opposed to allowing the tested code to make requests as it does in production. MSW does not suffer from this.
Additional context
MSW introduced a brand-new approach to API mocking by utilizing the Service Worker API. That allows your application to perform requests and receive responses and have control over the network without stubbing anything. The concept and implementation behind MSW had brought it "The Most Exciting Use of Technology" award on Open Source Awards 2020 and established it as a technology to assess by TechRadar.
I can't ignore the fact that the adoption of MSW in Create React App would increase its usage surface, which would help to discover more issues and usage scenarios, improving the developer's experience for all our users and the ecosystem in general. Perhaps such integration could also bring more collaborators to the project and ensure its bright future.
You can learn more about Mock Service Worker: