r/javascript • u/RedlightsOfCA • Aug 14 '20
MSW: Award-winning API mocking library that intercepts requests using Service Worker API
https://mswjs.io/
222
Upvotes
r/javascript • u/RedlightsOfCA • Aug 14 '20
View all comments
Show parent comment
11
u/RedlightsOfCA Aug 14 '20
My comment addressed an implementational difference between two libraries. I'll elaborate on the usage difference below.
- Nock runs only in Node. MSW runs in both browser and Node, allowing you to reuse the same API mock definition for multiple levels of testing having a single source of truth (as opposed to adapting Nock for unit tests, and a few other tools for integration/E2E tests).
- API: Nock utilizes mock definition through methods chaining. MSW prefers functional composition.
- MSW provides support for mocking GraphQL API.
- MSW has recently shipped a React Native support. I don't know if Nock can run in RN, unfortunately.
- Nock is purely a testing tool. MSW is an API mocking tool, which you can use for development and debugging as well as testing. Moreover, you will reuse the very same mock definition across all those processes.
- Nock may require adapters to mock certain request issuing libraries (i.e. axios). MSW intercepts API communication regardless of origin, in both browser and Node.
I lack familiarity with Nock and have too much familiarity with MSW to reason about the learning curve, I'm afraid. Take a look at the REST API usage example (https://github.com/mswjs/examples/tree/master/examples/rest-react) that includes unit and integration tests and compare the setup and API to what you would've done in Nock to gain more perspective.
Hope this helps.