How to setup MSW for Vitest browser mode? #2303
-
|
I'm trying Vitest browser mode (still experimental) and I'm wondering what's the canonical way to setup MSW in this mode? I tried: // src/vitest-setup.ts
import { worker } from "./mocks/browser";
import { cleanup } from "@testing-library/react";
// from https://mswjs.io/docs/getting-started/integrate/node#setup
// Establish API mocking before all tests.
beforeAll(async () => {
await worker.start({ onUnhandledRequest: "error" });
});
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => {
worker.resetHandlers();
cleanup();
});
// Clean up after the tests are finished.
afterAll(() => {
worker.stop();
});with: // vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
setupFiles: ["src/vitest-setup.ts"],
globals: true,
browser: {
enabled: true,
name: "chromium",
provider: "playwright",
},
},
});While this seems to work, I'm not sure this is 100% correct. Also, I am not able to override a request in a single test using: worker.use(...);
render(<MyComponent />);The override doesn't seem to have any effect or to be taken into account. EDIT: the override works if I move whatever I have inside |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 12 replies
-
|
Hi, @foxaltus. I suspect that setup files aren't applied in the browser. That's still the setup file for your Node.js run of Vitest. @sheremet-va, please correct me if I'm wrong. |
Beta Was this translation helpful? Give feedback.
-
|
I've added the Vitest Browser Mode recipe to the MSW docs. Please follow it to integrate MSW with Vitest Browser Mode. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
I'm not sure if this an issue of I can workaround this, if I actually retry the call (using tanstack query btw). I tried to debug for a bit, but without any conclusions. Seems like the worker is registered but somehow is not yet ready to handle the requests. Have you seen something similar to this? @kettanaito |
Beta Was this translation helpful? Give feedback.
-
|
I'm having some weird behaviour when tests are defined in the same file. If first test uses default handler it passes but next text that overrides fails as if it didn't override. But if I reverse the order, the second one (which is now first one) gets overridden well and passes and both pass. Does anyone have any idea why? |
Beta Was this translation helpful? Give feedback.
-
|
I'm also trying to use a similar setup of Vitest + @testing-library/react + react-query msw but I'm getting really inconsistent results for my tests. Considering switching to |
Beta Was this translation helpful? Give feedback.
-
|
I seem to have the same test pass outside of a describe but fail within a describe? Is there gotcha's for using 'describe'/suites? Moving the code out of the describe appeared to help here as I'm expecting From the console debug when its in a describe stdout goes to unknown test. Outside of describe the stdout is always referring to the test. with a describe |
Beta Was this translation helpful? Give feedback.
-
|
I'm using it too with And i keep getting these errors It seems pretty clear msw with vitest browser is still experimental. Most examples I see that are working still target jsdom. |
Beta Was this translation helpful? Give feedback.
I've added the Vitest Browser Mode recipe to the MSW docs. Please follow it to integrate MSW with Vitest Browser Mode. Thanks!