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

How to overwrite the handlers using the array definition? #114

Open
BrunoQuaresma opened this issue May 23, 2023 · 2 comments
Open

How to overwrite the handlers using the array definition? #114

BrunoQuaresma opened this issue May 23, 2023 · 2 comments

Comments

@BrunoQuaresma
Copy link

Hey folks, I have a few dozen handlers being used in MSW as an array and I see to overwrite it using the addon I would have to make them "named handlers" which from the setupServer() definition is not allowed. It would also require a high amount of effort to convert them into an object map.

So is there a way to overwrite the initial global handlers in the story similar to server.use(...)? So I would not have to change my handler's definition and be able to use them on Storybook and regular tests.

Thank you!

@BrunoQuaresma
Copy link
Author

Kinda of related #82

@kettanaito
Copy link
Member

Hi, @BrunoQuaresma. From my understanding of the Storybook merge algorithm, the array of request handlers you provide on the individual story level will take precedence over the base request handlers you provide to initialize(). Named handlers just allow you to disable a certain group of handlers altogether.

This is how I'd expect this to work:

initialize({}, [
  http.get('/resource', thisIsFallback)
])

export const YourStory = {
  parameters: {
    msw: {
      handlers: [
        http.get('/resource', thisTakesPrecedence),
      ]
    }
  }
}

@yannbf, please correct me if I'm wrong here. If I am, this is likely a bug and we need to look into this. Also, will check if we have tests for this use case.

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

2 participants