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.

mockServiceWorker.js resource remains in a"pending" status when I open up the Network tab

See original GitHub issue

Hi, I am using msw to mock fetch API request in my project, I check the network tab and the mockServiceWorker.js resource remains in a"pending" status, additionally, the response retrieves index.html instead of the JSON response I mock in the handlers file. Maybe I am incurring a silly error but I didn’t find some resource on the web that could help me, thanks in advance!

Environment

Name Version
msw 0.28.2
browser Chrome 90.0.4430.93
OS Windows 10

Request handlers

I run "dev": "webpack serve --mode development", in CLI and these are my configurations

// index.tsx
...
if (process.env.NODE_ENV === 'development') require('./mocks/browser');

const rootElement = document.getElementById('app');

ReactDOM.render(<ThemeProvider><App /></ThemeProvider>, rootElement);

// ./mock/browser.ts
import { setupWorker } from 'msw';
import { handlers } from './handlers';

const worker = setupWorker(...handlers);

worker.start();

// handlers
import { rest } from 'msw';

const baseURL = 'http://localhost:8080';

const handlers = [
  rest.get(`${baseURL}/navlink-routes`, (_req, res, ctx) => res(
    ctx.status(200),
    ctx.json({
      result: [
        { label: 'Shop', path: 'shop' },
        { label: 'Wishlist', path: 'wishlist' },
        { label: 'Checkout', path: 'checkout' },
      ],
    }),
  )),
  rest.get('*', (req, res, ctx) => res(
    ctx.status(500),
    ctx.json({ error: `Please add request handler for ${req.url.toString()}` }),
  )),
];

export { handlers };

Actual request

// Example of making a request. Provide your code here.
async function getData(): Promise<void> {
      try {
        const response = await fetch('http://localhost:8080/navlink-routes', { signal });

        if (!response.ok) throw new Error(`Server error ${response.status}`);

        const { result } = await response.json();
        setLinkList(result);
      } catch (e) {
        if (e.name !== 'AbortError') setError(true);
      }
    }

Current behavior

image

Expected behavior

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, May 4, 2021

Glad it got resolved, @david2am!

1reaction
david2amcommented, May 4, 2021

@kettanaito I wasn’t set up a public directory properly I was using my dist directory which I created for production purposes calling:

webpack --mode production

Following your suggestion, I created a new public/ directory and call:

npx msw init public

That solves the issue, thanks for your help 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

msw mockServiceWorker.js resource remains in a"pending ...
I am using msw to mock fetch API request in my project, I check the network tab and the mockServiceWorker.
Read more >
mockServiceWorker.js in pending state · Issue #1233 - GitHub
Prerequisites I confirm my issue is not in the opened issues I confirm ... remains in a"pending" status when I open up the...
Read more >
Debugging uncaught requests - Recipes - Mock Service Worker
You can verify that your Service Worker is registered at the root by accessing it: <SITE_URL>/mockServiceWorker. js , where SITE_URL is the ...
Read more >
Cloud Manager | Linode
Guides and tutorials on the Linode platform, Linux basics, and software installation and configuration.
Read more >
What's New with Cypress Web Testing?
Added a ready event to the Plugins process to ensure all plugins have successfully started before the configuration is loaded and the tests...
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