Install Mock Service Worker (MSW) and Prepare Directory Structure
lessonDuration 24s
Let's add `msw` to the project and prepare the basic directory structure with our `handlers.ts` where you will describe the network behavior you want.
Browse
Find courses, lessons, articles, talks, and podcasts.
42 results for "msw"
Showing 1–24 · Page 1 of 2
lessonDuration 24s
Let's add `msw` to the project and prepare the basic directory structure with our `handlers.ts` where you will describe the network behavior you want.
lessonDuration 3m 28s
As the name implies, Mock Service Worker (MSW) uses the Service Worker API to intercept requests in the browser. By doing that, it can achieve API mocking without patching "fetch" or affecting the source code of your app in any way—all by using the platform. Learn how to copy the worker script and register the service worker to have API mocking enabled in for client-side development. - [Browser integration](https://mswjs.io/docs/integrations/browser)
lessonDuration 1m 6s
Mock Service Worker relies heavily on the standard Fetch API classes like Request and Response when describing the network. But sometimes standards aren't enough. For example, you cannot mock a response cookie because JavaScript prohibits setting the "Set-Cookie" header on a Response instance. Learn how to side-step this limitation by using a new "HttpResponse" class from MSW. - [`HttpResponse` API](https://mswjs.io/docs/api/http-response)
lessonDuration 1m 50sFree
In this lesson, we include the `getServerSideProps` and `getStaticProps` exports into a story, using Storybook loaders. We mock the fetch request using MSW.
lessonDuration 2m 5sFree
In this lesson, we add the Mock Service Worker (MSW) library to Storybook for API endpoint mocking in Storybook. We configure MSW by hand for use with Next.js features like `getServerSideProps` and `getStaticProps` in stories.
lesson
Let's write an integration test for our `<Chat/>` component in Vitest and React Testing Library. The WebSocket interception in MSW requires a global `WebSocket` class. You are covered if you're using Node.js v22 or higher since that global class exists there. But for older versions, we will create a custom Vitest environment to polyfill it using `undici`. ## Resources - [Vitest](https://vitest.dev/) - [Undici](https://github.com/nodejs/undici) - [Testing Library](https://testing-library.com/)
lesson
With MSW, you can use custom close codes that are otherwise unusable on the client. For example, in this lesson we will emulate a WebSocket connection closure due to a 1003 status code, which means that the server is unable to process the incoming data. ## Resource - [`client.close()` API](https://mswjs.io/docs/api/ws#closecode-reason)
lesson
Event handlers in MSW represent a WebSocket server. And it so happened a single server may manage a communication across multiple clients. Let's see how we can close multiple WebSocket client connections at once! We can get access to the set of all connected WebSocket clients in the `chat.clients` property. From then, it's a matter of iterating over them and calling `client.close()` on individual clients. ## Resources - [`.clients`](https://mswjs.io/docs/api/ws#clients) - [`client.close()` API](https://mswjs.io/docs/api/ws#closecode-reason)
lesson
Now that we have the connection to the actual WebSocket server established, we can listen to any events sent from the server to the client. The thing is, our event handler in MSW will receive those events before the client does. ## Resources - [Intercepting WebSocket server events](https://mswjs.io/docs/basics/handling-websocket-events#intercepting-server-events)
lesson
Sicne we're involving an actual WebSocket server in our mock, what happens if that server disconnects? Well, the client connection will close, which is a bummer. Let's see how to listen and prevent original server's `close` event, and also conditionally fallback to MSW to act as the server if that happens. ## Resources - [`server.addEventListener()`](https://mswjs.io/docs/api/ws#addeventlistenerevent-listener-options-1)
lesson
When mocking a WebSocket connection in the browser, MSW takes charge of logging WebSocket events in the console. In this one, we explore the different log types and learn how to read the log output of your WebSocket links. ## Resources - [WebSocket logging](https://mswjs.io/docs/basics/handling-websocket-events#logging)
lessonDuration 1m 52s
The more your request handlers resemble the actual production behavior, the more confident you'll get when prototyping and testing with Mock Service Worker. But here's the thing—mocked responses arrive to the client **really quickly**. Learn how to control response timing and server-side latency by using the "delay" function. - [`delay()` API](https://mswjs.io/docs/api/delay)
lessonDuration 2m 2s
When mocking a collection of resources in Mock Service Worker, such as our movies, you will encounter requests that have dynamic parts in their URL, like `/movies/:slug`. In this lesson, learn how to use _path parameters_ to describe dynamic portions of the request's URL, access those parameters, and return a different response based on their values. - [Path parameters](https://mswjs.io/docs/network-behavior/rest#reading-path-parameters)
lessonDuration 2m 19s
Mocking authentication comes down to how it's implemented. With Mock Service Worker, you can mock the "Set-Cookie" response header to emulate a user session, but with Remix you don't even have to. Instead, in this lesson we will intercept only the authorization call to a database, leaving Remix to handle sessions, cookies, and auth state for us.
lessonDuration 1m 55s
Learn how to use the native `URL` and `URLSearchParams` classes to parse the intercepted request's URL and access its query parameters inside your Mock Service Worker handlers. - [Reading and writing request query parameters](https://mswjs.io/docs/recipes/query-parameters)
lessonDuration 1m 50s
Request handlers you write in Mock Service Worker apply for both Node.js and the browser simultaneously. In this lesson, you will handle a client-side request for movie recommendations using the same `http` namespace you already know. We will also take a sneak peek at the browser's Network to see what happens to the requests when we mock them.
lessonDuration 1m 47s
Let's use the "setupServer" API from Mock Service Worker to enable API mocking on the server-side of our Remix application. This way you can intercept and mock server-side requests, like those happening in the "loader" and "action" functions, as well as reuse the same setup for any other Node.js process, like integration tests with tools like Vitest or Jest in the future. - [Node.js integration](https://mswjs.io/docs/integrations/node)
lessonDuration 1m 33s
Error handling is crucial for a great user experience. Learn how to reproduce _any_ error response with Mock Service Worker, be it a 404, 500, or any other HTTP error response. - [Mocking error responses](https://mswjs.io/docs/basics/mocking-responses#mocking-error-responses)
lessonDuration 1m 34s
When working with existing APIs, especially those you don't own, being able to take the original response and manipulate it can be a life-changer when iterating on features or reproducing bugs. In this lesson, you will learn to perform the original request and then use Mock Service Worker to patch it with whichever data you want. - [`bypass()`](https://mswjs.io/docs/recipes/response-patching)
lessonDuration 3m 55s
Let's spice things up by adding GraphQL to our application. Since you are prototyping mock-first, you don't have to write any GraphQL schemas or define any resolvers. Instead, you will utilize Mock Service Worker to intercept valid GraphQL queries by their _type_ and _name_, and respond with the same semantic JSON responses matching the queries. - [Describing GraphQL APIs](http://mswjs.io/docs/network-behavior/graphql)
lessonDuration 2m 22sFree
You can send any data from your response resolvers. Let's implement a video streaming functionality by responding with a `ReadableStream` with Mock Service Worker. As a cherry on top, you will learn how to transform that video stream to inject server-side latency and see how our application's player handles it.
lessonDuration 3m 26sFree
Operation-based GraphQL mocking allows you to move fast without investing time into schemas or resolvers. But, when integrating Mock Service Worker into an existing application, going schema-first can be advantageous. Here's how you can route every GraphQL request to be resolved against a GraphQL schema, mocked or real, and use mock resolvers to control individual types and fields.
lessonDuration 1m 20s
Accounting for network errors is what makes a good application great. Unlike error responses, network errors represent issues when sending a request or receiving a response, but all the same can be mocked with standard JavaScript API. In this lesson, you will learn about `Response.error()` and how to use it to emulate network errors in your Mock Service Worker handlers. - [Network errors](http://mswjs.io/docs/basics/mocking-responses#mocking-network-errors)
lessonDuration 2m 15s
To control what happens to requests in Mock Service Worker, you need to intercept them first. Write your first **request handler** and learn how to intercept a server-side request our application makes in Remix's `loader`. And mocking a response? Using a Fetch API `Response` instance, of course! - [Describing RESTful APIs](http://mswjs.io/docs/network-behavior/rest)