question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request not intercepted when server started in start script

See original GitHub issue

Environment

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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kettanaitocommented, Jul 13, 2021

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.

// src/app.js
if (process.env.NODE_ENV === 'test') {
  const { worker } = require('./worker')
  window.worker = worker
}
// test/Login.test.js
beforeAll(async () => {
  await getWindow().worker.start()
})

getWindow() is your way to access the window, as it’s different depending on which testing framework you’re using. In Cypress that’d be cy.window(cb), for example.

Remember that worker.start() returns a Promise that you can await in your beforeAll hook.

1reaction
iperziccommented, Dec 8, 2020

@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 into http or https node modules didn’t work because no request was made from them.

Anyway, thanks for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

html - Intercepting and rerouting http request sent by javascript ...
I have a script from a cdn included in the html via the script:src tag. This script sends a request to a server...
Read more >
Chapter 4. Intercepting network requests - Progressive Web ...
Instead of the user making a request to the server, the Service Worker intercepts the request and decides to serve it from cache...
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
It's a very simple Vue.js app built with json-server - a single file json database. ... This is where .intercept() starts to show...
Read more >
Intercept HTTP requests - Mozilla - MDN Web Docs
To intercept HTTP requests, use the webRequest API. ... use onBeforeRequest to call the logURL() function just before starting the request.
Read more >
Intercepting JavaScript Fetch API requests and responses
const { fetch: originalFetch } = window; window.fetch = async (...args) => { let [resource, config ] = args; // request interceptor starts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found