-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
React: Add testing utilities #12959
React: Add testing utilities #12959
Conversation
examples/cra-ts-essentials/src/stories/Button.babelproposal.deleteme.tsx
Outdated
Show resolved
Hide resolved
72294ab
to
ddd4520
Compare
ddd4520
to
2e2b507
Compare
@yannbf I have this on my TODO list to take a proper look. I was a bit slammed at the end of the year and am away for another week but I should get a chance to look at it after that! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't check out the branch and run it locally, but I did copy/paste the composeStory
function into my project (along with some of the new/updated types) and it worked as expected.
Thanks, Yann!
Thanks for doing this! and thanks for the feedback :)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, this is great! One question
if (!passArgsFirst) { | ||
throw new Error( | ||
'composeStory does not support legacy style stories (with passArgsFirst = false).' | ||
); | ||
} | ||
return story(context.args as GenericArgs, context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!passArgsFirst) { | |
throw new Error( | |
'composeStory does not support legacy style stories (with passArgsFirst = false).' | |
); | |
} | |
return story(context.args as GenericArgs, context); | |
return passArgsFirst ? story(context.args as GenericArgs, context) : story(context); |
Was there a reason not to just do this? I'm not sure if it is a big deal to support non-passArgsFirst
but this is simple enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO we shouldn't support legacy CSF on new features. if you want to use it, migrate!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if we should resolve this conversation :D
I'm happy to do either way
Thanks for writing this PR, it's going to really help me out. |
Thanks for commenting! It's because of people like you that open source is worth doing 😄 |
90003dd
to
1330558
Compare
Co-authored-by: Aaron Reisman <aaron@lifeiscontent.net>
- also change the signature of composeStory and expose it
- globalConfig is now set in setup files, only once!
1330558
to
7ecc75b
Compare
Just wanted to check in here, is the plan still to get this released soon? I've been holding out on writing some unit tests because I wanted to try this out, but I'm not sure how much longer I can wait 😅 |
Hey Mark, thanks for this message! Currently we are discussing wether this feature would go in the repo or in a separate package. You will hear some news soon, thanks for your patience 🙏 |
this has been released as a standalone addon. closing! |
Hi @yannbf, https://storybook.js.org/docs/react/writing-tests/importing-stories-in-tests |
Issue: #10145
What I did
Add a testing utility to @storybook/react to compose a story with decorators and parameters so that it can be easily reused in tests with tools like testing library.
This allow for codes in test files that would otherwise have quite some boilerplate to be as simple as:
1 - with
composeStories
, composing all stories2 - with
composeStory
, if you wish to do so for a single storyGlobal config
If you have global decorators/parameters/etc and want them applied to your stories you need to first set it up. You can either do it in a setup file that should run once, before composeStory(ies) will be used, for instance:
Or you're free to use either API and pass a global config directly as a parameter, though I won't recommend because it requires more effort:
How to test
1 -
yarn bootstrap
oryarn build addons react --watch
if you prefer (addons just for the types, because react uses base types from addons)2 - go to
examples/cra-ts-essentials
3 - run
SKIP_PREFLIGHT_CHECK=true yarn test
(there's a compatibility problem with jest versions in the monorepo and the project, so just skip it for now and the tests should work)Yes
Yes, added in
examples/cra-ts-essentials
projectAdded, but I need feedback
Further work
It's quite interesting to bring this to other major frameworks like Vue and Angular, but the way the tests are written is quite different (every framework with their specific peculiarities) and it would require further research and specific setup to allow them to take advantage of a testing utility like this one. I believe we should release this for React only first, get some feedback and then start working on other frameworks.