Request not intercepted when server started in start script
See original GitHub issueEnvironment
Name | Version |
---|---|
msw | 0.22.0 |
node | v12.14.0 |
OS | 10.15.7 (Catalina) |
Background
We are trying to mock API calls when running behavioral tests with mocked API through webdriverio. First, we encountered an issue that wdio would hang forever and no test would get executed when starting MSW server in wdio config file. We thought that this was due to the fact that wdio and serving the app have been ran as separate node processes, but upon changing the configuration so that app is built, and served through express server, this issue is present.
Moving to client mocking is not an option for us, because some API calls are being made before React app has been bootstrapped, and we’d need to mock those requests as well.
NOTE: The full example project, which is essentially ejected CRA + msw installed, can be found in this repo
Request handlers
const { rest } = require('msw');
const { setupServer } = require('msw/node');
const server = setupServer(
rest.get('http://localhost:3000/test', (req, res, ctx) => {
console.log('hit');
return res(ctx.status(404));
})
);
server.listen()
Actual request
fetch('/test')
Current behavior
The server responds with 200 status code, and no unhandled requests are logged. Nothing is logged as a matter of fact, except usual CRA stuff.
The same behavior can be seen when building an app, and then serving the production version via an express server.
Expected behavior
The mocks should catch the requests coming from the app.
Screenshots
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Hey, @hammidf.
You can set the
worker
as a global runtime variable in your app when testing, and then start the worker from your test.Remember that
worker.start()
returns a Promise that you can await in yourbeforeAll
hook.@kettanaito sorry for the long wait.
The thing is that I was making requests from the browser environment and trying to intercept the requests in a separate NodeJS process. In this case it was a webdriver.io instance, which obviously can’t tap into browser’s
fetch
, and tapping intohttp
orhttps
node modules didn’t work because no request was made from them.Anyway, thanks for your help!