Skip to content

MSW produces 404 error responses for valid mocked requests after you restart the worker in the browser #2602

@spocke

Description

@spocke

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 20 or higher

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

https://github.com/spocke/mswjs-stop-bug/tree/bug/404-after-stop

Reproduction steps

This one is very simple to reproduce just start one mock, then stop that mock then start another and that mock now returns a 404 even if it has a valid mock handler in place.

  1. Clone the repo and go to the 404-after-stop branch
  2. Install the npm dependencies using npm i
  3. Start the server using npm start
  4. Open the Chrome or Safari and go to go to http://localhost:3000
  5. Load the page
  6. Observe that second mock request produces a 404 error

This is the code to reproduce the issue we just setup a worker1 do a mocked plain text request then stop that worker. Then we setup a second worker and do another mock request this one produces a 404.

  const worker1 = setupWorker(...[
    http.get('/foo', async ({ request }) => HttpResponse.text('hello 1', { status: 200 }))
  ]);

  await worker1.start({
    serviceWorker: {
      url: '/mockServiceWorker.js'
    }
  });

  await fetch('/foo').then(res => res.text()).then(console.log);

  worker1.stop();

  const worker2 = setupWorker(...[
    http.get('/foo', async ({ request }) => HttpResponse.text('hello 2', { status: 200 }))
  ]);

  await worker2.start({
    serviceWorker: {
      url: '/mockServiceWorker.js'
    }
  });

  await fetch('/foo').then(res => res.text()).then(console.log); // This will produce a 404

  worker2.stop();

It can be reproduced in Chrome, Firefox and Safari using the latest 2.11.3 version.

Current behavior

The second request produces a 404 error even if it's a valid mocked request.

Expected behavior

That the second request returns a properly mocked response like the first request but with the new expected response from the new mock handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds:triageIssues that have not been investigated yet.scope:browserRelated to MSW running in a browser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions