Set Up MSW in Node.js

Share this video with your friends

Social Share Links

Send Tweet
Published 7 months ago
Updated 7 months ago

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.

Instructor: [0:00] There are no service workers in Node.js, but we can use MSW there all the same. Let's create a new node module where we will set up MSW in Node.js. In the node module, import { setupServer } from 'msw/node'. Create a new server variable, and assign it the result of the setupServer function call.

[0:19] The setupServer function will return an object that we can use to start, stop, and control MSW in Node.js. Let's export the server object so we can reuse it in any Node.js process, such as our development server in Remix or integration tests with tools like Vitest or Jest.

[0:36] Similar to the setupWorker function, the setupServer expects request handlers. Let's import them from the same handlers module. By relying on the same list of handlers, we introduce a single source of truth for our network description and then utilize functions like setupServer and setupWorker to let MSW establish API mocking correctly in different environments for us.

[0:57] The Node.js setup is ready. All we'll have to do is start it. To do that, go to the server entry of our application. In Remix, that's an entry.server module. Import the server object from the Node.js integration we've just created. Then, call server.listen() to enable API mocking.

[1:19] Notice that this method is synchronous, and it will affect the current process as soon as it's called. Despite the name, the setupServer API does not create any actual servers, and instead, extends the standard Node.js APIs like HTTP to provision request interception.

[1:36] As the last thing, let's make sure that we enable API mocking only while in development by checking the node environment variable on the process and calling server.listen() conditionally.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today