Integration with Node.js #2050
-
Hello, I am trying to integrate MSW with Node.js. My assignment is to steup MSW, as a standalone mock server, using express. Then have Angular frontend tests when run, can call that mock server. I follow the instructions of Node.js integration in the official docs. So first I installed the MSW, running following command in the main folder of node application:
Then I defined a simple handler.js file:
Then I setup the MSW server in node.js file:
And finally in index.js I start the node application and start listening for requests:
But unfortunately when I run the application, open the browser and go to localhost:3000/ or localhost:3000/movie, As I am new to Node and MSW, I don't know what I am doing wrong. I was looking at official examples, but couldn't find any with node integration. Thanks for any help. EDIT 1Thanks Artem! I switched to http middleware setup instead, it works !!! here is what I have. My handlers still the same as they were:
My node.js which setups a standalone express server, but much simpler and provided by MSW:
And finally my main application index:
And when go to localhost:300 or localhost:3000/movie, get the mocked responses :) Edit 2Only one thing is still unclear to me. There are two separate projects, one is above Node mock application. The other is the real Angular frontend application. How can I setup MSW server, from the Angular test setup ?
How can I make this setup, if both mock and frontend are separate projects ? Also what is the difference between createServer() and createMiddleware() in @mswjs/http-middleware ? Thanks for any help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, @mirmak4. What you are missing is this part:
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)
Then, make sure you start this server before your tests. You can do so however you chose (e.g. using the |
Beta Was this translation helpful? Give feedback.
-
Thanks Artem, it works! May I have additional question, really sorry for spamming you with all those questions, What if additionally to the above localhost handlers (for the local application), Let's say that the frontend application at some point has to call some external service, How could this be achieved, preserving the current localhost setup as well ? I tried to mock the Could this be achieved by adding the core MSW dependency, and using |
Beta Was this translation helpful? Give feedback.
Hi, @mirmak4.
What you are missing is this part:
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: