Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The request from msw cannot be returned normally in browser mode when using vi.mock #7290

Open
6 tasks done
e7h4n opened this issue Jan 19, 2025 · 0 comments
Open
6 tasks done

Comments

@e7h4n
Copy link

e7h4n commented Jan 19, 2025

Describe the bug

When a request is intercepted by msw, the result returned by msw should be accessible in the test. However, if vi.mock is used in the test, the request intercepted by msw will be hung in the browser. This issue only exists in browser mode.

I am not sure if this issue is the same as #6540 and #6766, but it seems to be somewhat related.

update 1:
#6766 exactly the same problem, and there is another issue thread in msw

Reproduction

Reproduction is simple, just use vi.mock to ANY module will prevent msw return response.

The following codes will not work:

export const worker = setupWorker(
  http.get("https://example.com/user", () => {
    return HttpResponse.text("ok");
  })
);

it("should work with msw", async () => {
  await worker.start();
  vi.mock("empty-npm-package"); // this line will prevent msw return response

  const resp = await fetch("https://example.com/user");
  expect(resp.ok).toBeTruthy();
});

System Info

System:
    OS: macOS 15.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 18.92 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.6.0 - /opt/homebrew/bin/node
    Yarn: 1.22.4 - /opt/homebrew/bin/yarn
    npm: 10.8.2 - /opt/homebrew/bin/npm
    pnpm: 9.14.4 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.265
    Safari: 18.1
  npmPackages:
    @vitest/browser: ^3.0.2 => 3.0.2 
    vitest: ^3.0.2 => 3.0.2

Used Package Manager

pnpm

Validations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant