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.

Function use() not work in browser

See original GitHub issue

Environment

Name Version
msw 0.36.8
browser Chrome

Request handlers

browser.ts

import { rest, setupWorker } from 'msw';

const worker = setupWorker(...);

window.msw = {
    worker,
    rest,
};

export { worker };

worker.ts

import { worker } from '../../mocks/browser';

export const setupMsw = () => {
    return worker.start({
        onUnhandledRequest: 'bypass',
        serviceWorker: {
            url: process.env.CI ? SOME_PATH/mockServiceWorker.js : '/mockServiceWorker.js',
        },
    }).then(() => {
        window.msw.ready = true;
    });
};

Current behavior

  1. Open the page where mock url uses
  2. Check that mock url fired correctly and mock data received successfully
  3. Open browser console and try to change mock data with use() function
window.msw.worker.use(
    window.msw.rest.get('**/rest/shops', (_req, res, ctx) => {
        return res(
            ctx.json([]),
        );
    }),
);
  1. Try to use printHandlers() function. I see old and new mock urls
window.msw.worker.printHandlers()

[rest] GET /rest/shops
—- SOME OTHERS —-
[rest] GET /rest/shops
  1. Try to fetch mock url one more time in browser console
fetch('/rest/shops')
  1. I can see 2 debug messages in browser console (old and new mocks fired)
[MSW] 14:01:37 GET /rest/shops (200 OK)
[MSW] 14:01:37 GET /rest/shops (200 OK)
  1. Response from fetch are equal old mock (not new from use() function)

Expected behavior

  1. Response from fetch are equal new mock

I try to write e2e test with PlaywrightJs and in my test use() function working not correct. So, I try to reproduce my case in browser and I see the same error which I described earlier.

Could you help me with this issue Thank you!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
kazeichevcommented, Feb 28, 2022

@florianmatz Yes, I have got two workers.start() in different place of app.

0reactions
florianmatzcommented, Feb 27, 2022

@kazeichev Mmhhh… And how did you find out? Did you have two worker.start() or something?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS function works in all browsers but not all machines
1 Answer 1 · open the dev tools · switch to the Performance Tab · click "Record" (top left round icon) · do...
Read more >
Handling common JavaScript problems - MDN Web Docs
This includes information on using browser dev tools to track down and fix problems, using Polyfills and libraries to work around problems, ...
Read more >
Differences between using a workbook in the browser and in ...
A workbook in this format can be opened but macros do not run in a browser window. ... Most Excel functions work in...
Read more >
Activate JavaScript in your browser - Google AdSense Help
Open Chrome on your computer. Click More and then Settings. Click Security and Privacy. Click Site settings. Click JavaScript. Select Sites can use...
Read more >
How to enable JavaScript in your browser and why
Nowadays almost all web pages contain JavaScript, a scripting programming language that runs on visitor's web browser. It makes web pages functional for ......
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