[RFC] each and matrix tests
#32559
mrginglymus
started this conversation in
RFC
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
The new CSF testing feature (#30119) adds a familiar testing syntax to CSF factories, allowing users to write tests in storybook.
It's a huge improvement and a welcome feature, but inevitably there are features that other testing frameworks have that are not yet present.
This RFC covers the lack of
.each(or similar) functionality, to allow for easily parameterised tests.Problem Statement
A storybook component is likely to have several parameters to configure its appearance and behaviour.
Before CSF tests, for chromatic users, this meant a lot of copy + pasting to achieve the required combinatorial set of stories.
CSF tests allow for a slightly more refined approach, but owing to the static analysis requirement for indexing, the usual approach of dynamically generating variations at runtime is not possible.
Non-goals
No response
Implementation
This RFC proposes the addition of two new test factories on the
metaobject.The
eachfactory will accept a list of parameters to be applied to tests:The
matrixfactory will accept a list of parameters to generate combinations to be applied to testsThe parameters would also be available to customise the story options, so that they can be used to customise story args and parameters:
Note that the APIs here are somewhat speculative - the factories could easily be any of the following:
and many more
Indexing
In order to ensure tests are distinguishable, users will be able to need to inject parameters into them. However, as the test names must be statically analysable, this cannot be done at run time. A simple solution is to use a
sprintflike syntax:Note that this also requires the parameters to be inline rather than references.
Prior Art
The
eachmethod is an fairly common pattern in testing libraries:even though the same could often be achieved with a simple for loop
The
matrixmethod is less common - again, due to the dynamic nature of most test runners, the combinations can simple be passed toeach. However, it's quite common in CI:In addition, chromatic offers story modes which offer a similar functionality, but only at the global level.
Deliverables
No response
Risks
No response
Unresolved Questions
.test, etc)Alternatives considered / Abandoned Ideas
For runtime, it is technically possible to return a custom
previewthat will generate custommetato allow for custom runtime objects, though this would be extremely fragile.For indexing,
experimental_indexerswould allow detection of these stories; however, this may lead to a fair amount of duplication.Beta Was this translation helpful? Give feedback.
All reactions