Is there a way to perform setup and cleanup on a story in Storybook?
Asked Answered
D

1

7

Is there a way to execute setup and cleanup steps for a story in Storybook? Say, for example, I have a component that behaves differently contingent on the presence or value of some piece of local storage, or a property set on window. I would like to be able to setup a story so that but also cleanup so that it does not inadvertently carry over to other stories.

If there is no official suggested best practice, I would think the best way to do this is to make a custom decorator that wraps the story in a component with setup and teardown lifecycle events (I am using Storybook with React in this case, FYI). Open to other ideas.

Decani answered 16/12, 2021 at 18:20 Comment(0)
M
0

Decorators are the official recommendation for what you are describing. They can be added to an individual story, all stories for a component, or globally for all the stories.

Docs: https://storybook.js.org/docs/6.5/vue/writing-stories/decorators

Mesmerize answered 15/8, 2023 at 19:16 Comment(1)
we can use decorators to do setup work, but what about cleanup?Wantage

© 2022 - 2024 — McMap. All rights reserved.