Skip to content

Integration with Node.js #2050

Closed Answered by kettanaito
mirmak4 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @mirmak4.

What you are missing is this part:

as a standalone mock server, using express.

MSW on its own never spawns any servers. But you can take the request handlers you've defined and create an HTTP server out of them using the @mswjs/http-middleware package.

Add this file to your setup:

// mock-server.js
import { createServer } from '@mswjs/http-middleware'
import { handlers } from './handlers.js'

const httpServer = createServer(...handlers)
httpServer.listen(3001)

You don't need the file with server.listen() and server.close() at all in your setup. You can create that file if you are planning on using MSW in a Node.js process without any servers. That would be a common testin…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mirmak4
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants