-
Notifications
You must be signed in to change notification settings - Fork 601
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 automatically test storybook components using Backstop? #1279
Comments
Question here to clarify the problem since I am not a storybook user. Let's say you have a storybook with a custom menu dropdown component. In storybook -- how do you test states currently? Are there already something like integration tests for testing click & hover states etc? |
We do it in jest unit tests, testing that components have the correct behaviour on click and hover. |
However, Storybook is not used for testing - it is more like visual and simple storage of components. There are no "proper" integration tests, the only thing you can do there is to create another story of the same component and apply visual testing on it: https://storybook.js.org/docs/react/workflows/testing-with-storybook |
How are the jest tests integrated into storybook? In ember, when you generate a component, a test partial is also created along with the component scaffolding. When the test suite is run, all partials are imported and aggregated so they run together. Is this similar in storybook? And does this kind of integration make sense in a storybook world? |
The only thing you can do with Jest & Storybook is to use snapshot testing: The integration tests (with clicks and hovers) have to be done separately using only jest, where you access components directly (no Storybook is used). Storybook serves as visual storage of components and only works well with visual (snapshot) testing |
ok. I see. then it sounds like the optimal solution would allow you to use backstop in a similar way to jest? would that be the goal that would best solve this problem? |
That would be fantastic if you could implement something similar to: Pretty sure it will boost the popularity of BackstopJS |
@Denis-Evseev Yes, something like this seems very possible. This can potentially be modeled after the storyshots-puppeteer approach here. I quickly looked through the code -- it looks like this could be rewritten to wrap backstop instead of puppeteer. If that were to be done -- a backstop helper would wake up during a test run and test each story as a scenario. After all story tests are run a backstop report would be available like normal. Backstop would be configured via a modified backstop config file. Backstop has a dynamic test creation mode (where there is a config file with no set scenarios -- scenarios are instead generated on the fly (by passing in URLs, scenarioLabels and testIds) to support other test runners. see here some example code. It looks like the storyshots-puppeteer flow would contain the required metadata to enable this mode. This is similar to how the https://github.com/garris/ember-backstop solution works. I won't have time to implement this myself -- but If you want to take a look at this I can certainly support your investigation. |
Thank you @garris for the suggestion. Managed to get it to work in my project by following steps:
and the actual config file:
|
Looks like a great solution! What context do you run npx sb extract from? I assume this creates artifacts? I am curious about the workflow. Do you bundle this flow in some node project -- or the command line? How would this look in a CI environment like GH actions? |
I am running it in React + Storybook project using the command line. This is how storybook-static looks like: Haven't started working with CI - can't answer it yet but should not be a problem, in my opinion |
If you are using an older version of storybook that doesn't generate a json, or just want an alternative approach: Open storybook. Iterate through the menu items. "Click" on each item to expand it. That loads all submenu into the dom. Query those subitems. Iterate over and generate backstop scenarios. |
@Denis-Evseev I'm trying to do something along these lines: We're building web components, not react, using storybook book for documentation, and using backstop for regression. I'm using the iframe url for my scenarios, which isolates the component, but i can't seem select the components for hover/click did you run into this? |
@agent-simon-s you are asking Denis but figured I might suggest a few things ( aside from perhaps worth a different ticket ).
|
Thanks Stoyko Stanchev carry on ;> |
Thanks @Denis-Evseev ! Worked like a charm |
Just FYI to anyone who needs it... We have a number of stories that use the args settings to pass in variants for components. Writing stories for each of those clutters the StoryBook UI (for instance, button enabled/disabled x4 variants you end up with 8 stories where a single story will suffice for developers). In this case, we found using the ignore story useful (https://storybook.js.org/docs/react/api/csf#non-story-exports) to export stories that only appear in the extract but not in the UI. So we can write VRT stories but not expose them to the UI. |
Does anybody know what is the best way to test Storybook components with BackstopJS?
How to use Storybook metadata to generate a config which will test all components in a storybook?
Found an interesting solution that might help:
https://stackoverflow.com/questions/50408579/backstopjs-set-common-selector-for-all-scenarios/51223140#51223140
Here a guy managed to properly isolate components but still doesn't give an answer of how to create a config with all available components in a storybook.
The text was updated successfully, but these errors were encountered: