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.

Can't override existing handler (worker.use)

See original GitHub issue

Environment

Name Version
msw 0.38.1
browser Chrome
OS Mac OS

I’m trying to mock out a Auth-Flow, where I want to change a handler dynamically on /login-Route. The component mounted on /login should change the existing handler from status 401 to 200.

However, worker.use(…) does not override. It seems to ADD another handler. If I log worker.printHandlers(); I see two handlers. One with the desired status of 200, but still a handler returning 401.

Both have the same url (/auth), but only the last one (401) seems to take action in the end, so I can’t mock out my flow.

Request handlers

import { SetupWorkerApi, setupWorker, rest } from 'msw';

let wrk: SetupWorkerApi = {} as SetupWorkerApi;

// Conditionally setup msw to keep it out of vite production bundle
if (import.meta.env.DEV) {
    const handlers = [
        rest.get('/auth', (_req, res, ctx) => res(ctx.status(401))),
    ];

    // Configure a Service Worker with the given request handlers
    wrk = setupWorker(...handlers);

    // Store msw on window to access it in e2e-test
    window.msw = {
        worker: wrk,
        rest,
    };
}

export const worker = wrk;

Login component, that should change the /auth-handler

const Login = () => {
    const [workerChanged, setWorkerChanged] = useState<boolean>(false);

    useEffect(() => {
        const { worker, rest } = window.msw; 
        worker.use(
            rest.get('/auth', (req, res, ctx) => res(ctx.status(200)))
        );
        worker.printHandlers();
        setWorkerChanged(true);
    }, []);

    return workerChanged ? <Navigate to="/" /> : null;
};

Current behavior

Override not working, initially described. The following screenshot shows the two handlers logged with worker.printHandlers();

image

Expected behavior

  • Handler should be overridden, not added.
  • Handler should return the desired 200 status
  • worker.printHandlers(); should only show ONE handler

May be the same issue like in #1084.

I made sure I have only one MSW-instance running, at least I only have one “Mocking Enabled” log. Using vite as a bundler (HMR in dev mode) btw.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
florianmatzcommented, Mar 1, 2022

I changed the location of the app, but not with the browser router. That resulted in a complete refresh and thus creating a weird race condition between a useEffect and setupWorker.

1reaction
florianmatzcommented, Feb 28, 2022

Thank you for the fast reply. Found my issue^^.

Read more comments on GitHub >

github_iconTop Results From Across the Web

use() - Api - Mock Service Worker Docs
Request handler override can be used to handle only the next matching request. After a one-time request handler has been used, it never...
Read more >
Can't override an event handler when changing the argument ...
The best way to do it is to simply declare a new event with a different name. Hiding base class members is almost...
Read more >
Resolve the role_arn error when updating or deleting an AWS ...
Override the current IAM role used by AWS CloudFormation. 1. To update the stack, run the following command: aws cloudformation update-stack -- ...
Read more >
Using Service Workers - Web APIs | MDN
To override this default behavior and adopt open pages, a service worker can call clients. claim() . Whenever a new version of a...
Read more >
Use dependency injection in .NET Azure Functions
Overriding services provided by the host is currently not supported. If there are services you want to override, create an issue and propose ......
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