Playwright+Nock: Possible to start server within the same process as nock? #2480
Replies: 4 comments 3 replies
-
It should be possible, I've done it plenty of times. Note that |
Beta Was this translation helpful? Give feedback.
-
I just got a tip from a colleague that they've managed to use Playwright with serverside mocking. This basically allows you to do stuff like
where My colleague managed to run their node app and mocking lib within a context that can be exposed to Playwright, and ensures that everything runs within the same process. My colleague uses https://www.npmjs.com/package/msw as a mocking lib, but hopefully a similar concept should be doable with EDIT: or maybe not. Seems like Used kinda like this
And according to old issues like #1911 I see I'll post an update later if this turns out to be a dead end or a solution. |
Beta Was this translation helpful? Give feedback.
-
I've never used Playwright, so I'm making a guess by just looking at the express/nock setup you have in the example. I don't see anywhere where you're making requests to any host except local. So I wouldn't expect Also don't forget about Nock's debug mode to identity why requests weren't matched. |
Beta Was this translation helpful? Give feedback.
-
I've concluded that Playwright+Nock probably isn't possible. Went with Playwright+MSW instead |
Beta Was this translation helpful? Give feedback.
-
I'm writing my first Playwright test for my serverside-rendering node application.
Struggling with how to start my node application in a way that makes it run within the same process as the
nock
.The most common Playwright tests seem to use
page.goto("/")
to perform a request, and this assumes that a server has already been started.Here's an example Playwright-test that starts the node server via
http.createServer(app)
:In this test the nock doesn't apply to requests from the node app. I guess this is because
http.createServer(app)
starts the node app as a separate process, while nock is running on another process.I've looked into alternative ways of starting node apps via Playwright, one alternative is to let playwright.config.js start the node process, as described in https://playwright.dev/docs/test-configuration. But that would of course also start the node app as a separate process so it doesn't help
I am confident that my node application source code is compatible with
nock
as long as they're running within the same process. Because I've got a large well-established test suite for the same node application where I successfully usednock
in combination withsupertest
In this existing test-suite I can pass my application to supertest via
supertest(app).get("/")
and that makes my node app run within the same process as the nock.Example of supertest code
This works great. Requests from my node server do respond with fakeData accoding to my nock.
Wondering if someone knows
a) Different way to start my node app within the same process, rather than
http.createServer(app)
b) Different Playwright methods of using a node app within the same process as the test code
c) Different way of using
nock
d) Confirm that it's simply a dead end to attempt to use Nock in combination with Playwright for a serverside rendered node app
PS: If I only needed to mock clientside requests (requests from the browser) I know Playwright provides it's own mock methods, https://ray.run/blog/how-to-mock-http-traffic-in-playwright-test. But as far as I can tell these are just for clientside/browser requests so they are useless for mocking internal serverside requests from within my node app.
DS: The main reason I'm writing this question here in github.com/nock is that I've got a ton of nock-code already thanks to my existing test-suite and I'd like to reuse it. While Playwright is new to me.
Beta Was this translation helpful? Give feedback.
All reactions