mswjs / msw-storybook-addon

Mock API requests in Storybook with Mock Service Worker.
https://msw-sb.vercel.app
MIT License
407 stars 39 forks source link

onUnhandledRequest drastically increasing execution time #37

Closed stephan-noel-primer closed 3 days ago

stephan-noel-primer commented 3 years ago

Problem

Execution of visual tests on Chromatic goes from 3 minutes on average to around 18 minutes

Changing the following code:

 initializeWorker({
   onUnhandledRequest(req) {
     // Only some API requests should be mocked
     if (!req.url.href.startsWith(myapi(''))) {
       return;
     }
     console.error(
       'Found an unhandled %s request to %s',
       req.method,
       req.url.href,
     );

    throw new Error(
      'Unhandled request, please ensure you are mocking the correct endpoints',
    );
  },
});

to simply:

 initializeWorker();

fixed the issue.

I'm not sure if the problem is with this addon, with msw itself, or with how the integration is being done. If I want to workaround this and provide a fallback request handler to setupServer, I can't because there is no way to forward options to setupServer.

Version info:

msw-storybook-addon: 1.1.0 msw: 0.30.0:

kettanaito commented 3 years ago

Hey, @stephan-noel-primer. Thank you for reaching out.

I can suspect the suggestions logic in MSW that can be the culprit here. Whenever there's an unhandled request, MSW looks up a list of suggested request handlers to account for typos and method/operation kind mismatches to improve the DX. This can be an expensive operation given a large number of handlers.

Your approach above is sensible. Perhaps we should consider disabling the handler suggestions as an option if that's proven to be the performance bottleneck. To debug this, I recommend forking MSW, removing the suggestions, linking it to the msw-storybook-addon and using its local built to see if there's any performance impact.

kettanaito commented 3 days ago

MSW has long stopped shipping js-lavenshtein for request handler suggestions. Using onUnhandledRequest does not affect MSW performance now.