[RFC] Storybook MSW add-on #30267
Replies: 5 comments 3 replies
-
The majority of the points above is the result our prior discussion with @yannbf, to whom I am greatly thankful! |
Beta Was this translation helpful? Give feedback.
-
This looks great! Seems like a nice balance of MSW and Storybook patterns. I'm concerned that FYI, there is an API to pass options to an addon when registering it, which you can see here: https://storybook.js.org/docs/essentials#configuration. Unfortunately, it looks like we haven't documented how an addon author can set that up. It appears that they're passed in to preset functions? Here's the docs addon source showing that usage. |
Beta Was this translation helpful? Give feedback.
-
Hi @kettanaito ! This is so exciting -- it's great to see MSW get cleaned up and improved alongside Storybook's own efforts to improve component testing. Since mocking is critical to Storybook, I hope that this conversation can not only improve the addon but also improve Storybook's approach to mocking and data handling! InitializationAutomatic initialization certainly simplifies things. Is there nothing that the user would want to specify during initialization? Will there not be in the future either? I'm not familiar enough with MSW, so I want to double check. Handler definition
Disagree with this. MSW handlers belong in In Storybook If there was a way to do this mapping that led to a nice way to interactively edit your mock data in Storybook's UI, we should explore this direction together. The team has lots of ideas about how this might work. If you believe that tests should be as simple as possible, and that adding mock data editing violates that objective, then I can also appreciate that point of view, especially for v1 of the addon. Storybook config.meta({
parameters: {
handlers: [...userHandlers, ...checkoutHandlers]
}
}) Inheritance
I agree with this. We are generally moving away from "magic" in Storybook, and your proposal is consistent with this. |
Beta Was this translation helpful? Give feedback.
-
@kettanaito I wondered about your comment:
Are "happy path handlers" like global handlers that you would want to apply to all stories? If so what is the recommended syntax for adding them? Would it be a |
Beta Was this translation helpful? Give feedback.
-
I'd love to see an easy way to mock a certain endpoint per story. The last time I looked at msw it worked on a global basis and when I want to embed multiple different storybook examples on the same page I'd like to return different mock data even if the same endpoint is requested. Currently we use axios-mock-adapter for this, but a more native solution would be nice. |
Beta Was this translation helpful? Give feedback.
-
Summary
This RFS aims to improve the user experience of the msw-storybook-addon by aligning it with the new CSF and other things.
Problem Statement
initialize()
step.test()
function, it all feels disjoined.Non-goals
Implementation
This RFC includes multiple changes to the add-on.
Add-on renaming
This is irrelevant in the context of Storybook, but I am considering renaming the addon from
msw-storybook-adddon
to@mswjs/storybook
. We are likely to provide more targeted integrations with tools, like Storybook, Playwright, or Cypress, in the future. I would like for this add-on to be one of the first adopters of this naming pattern to shape a great user experience.Initialization
Initialize add-on as a plugin.
This will wrap all the necessary steps for the add-on to apply itself depending on the environment (browser/Node.js). No
initialize
call needed.Default usage
By default, the user provides request handlers as arguments to their stories. Arguments represent props of a story, and network description can be treated as one.
Defining request handlers on the story args serves as happy-path handlers that all variants of this story will automatically inherit and abide by.
Handler overrides
You can then override the base request handlers on a per-variant basis (or in the test function) like so:
Alternatively, you can grab a reference to the
SetupApi
instance in thebeforeEach
hook:Handler inheritance
I highly suggest provide a familiar handler inheritance experience as it is in the MSW usage outside of Storybook:
This point advocates for deprecating named-object handler groups as that's a behavior specific only to Storybook. It creates an experience that doesn't exist outside of Storybook, which is confusing and has a detrimental effect on people learning these two tools.
I've already showcase the handler overrides scenario above.
Here's an example of combining multiple handlers in a single story:
Disabling a handlers group
In the past, you could disable a certain group of handlers by providing
null
as the value to their key/value pair in the named-object configuration.I find that to be convoluted. Instead, you should compose the array of handlers that are relevant to the current story. Do not put all handlers in the base
meta
to then exclude some of them in particular stories. That makes you reference all around the file just to understand what handlers apply for a particular story.This is the recommended way:
There is no price to doing this, and explicitness always wins.
Configuring setup
You can customize the
SetupApi
instance like so:Beta Was this translation helpful? Give feedback.
All reactions