mswjs / examples

Examples of Mock Service Worker usage with various frameworks and libraries.
683 stars 211 forks source link

Not intercepting initial requests in Vite #119

Closed YuesIt17 closed 2 months ago

YuesIt17 commented 2 months ago

Hello! I have a problem with Msw + vite.

I have tried install latest version 2.4.2 and msw doesn't watch mock of api, i.e. i can not get into the handler of mock. But I see in console "[MSW] Mocking enabled" and if you request the api again, then i get data from mock handler.

Example with bug vite_msw

If you install for example "msw": "2.0.4", i don't have this problem vite_msw_2

kettanaito commented 2 months ago

Hi, @YuesIt17.

Your first example shows that you forgot to await worker.start(). Once you do, all the requests will be intercepted and mocked as you expect. Here's the fix:

// main.jsx
-worker.start()
+await worker.start()