[RFC] Args Mocking via ArgTypes #23045
Closed
vanessayuenn
started this conversation in
RFC
Replies: 1 comment
-
@kasperpeulen is writing a new proposal that shall supersede this RFC. The new one should be posted early next week. Closing this discussion for now. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Status: Proposed
Champion: @kasperpeulen
Authors: @tmeasday @kasperpeulen
Summary
This RFC proposes a solution to providing mocked arg values for callbacks that doesn’t require inference, at the cost of being manually configured. Ideally:
Problem Statement
Automatic arg mocking is a neat feature that we shipped in Storybook 6.5. We automatically analyze your components, figure out function inputs, and then automatically generate
actions
that get passed to your stories. From this, you get:actions
panel for debuggingplay
function tests (e.g.expect(onClick).toHaveBeenCalled()
)Unfortunately, this “magical” feature has led to a number of unsolved problems that complicate/block key initiatives. If we had an ergonomic way for users to specify these arg mocks as part of their story definition, we could unblock all of these other projects.
Non-goals
A fully fleshed out solution for current non-features such as network and import mocking (although let’s consider them).
Implementation
Streamlined argType API, special
SBScalarType
for actionscolor
?)Prior Art
Current
ArgType
TS type:Current API for mocking a single arg:
Current automatic actions mocking API
Deliverables
ArgType
to allow strings, updatenormalizeInputType
function to handle shorthand{ type: ‘function’, returnType: ‘undefined’ }
(Or should it just expand to{ type: 'event' }
?)README
s.argType
definition optional.Risks
No response
Unresolved Questions
No response
Alternatives considered / Abandoned Ideas
Inference based on prop types
The current system uses inference on the name of the arg and a configured parameter. It’s zero config as the arg name is inferred. However it both (a) requires inference, and (b) is a challenge for TS as the parameter can be changed and is not possible to access from TS.
An alternative proposal would be to infer the type of the arg and if it is a non-valued function, to automatically mock it (as justified elsewhere).
The downside of this approach is that it still needs inference, and in fact it requires a more reliable inference than just detecting the name.
Other ways of manually specifying args
Keep matching on name, ask users to name their mocked args
Just encourage people to use the current API
Function type based matching without inference
Parameter based?
zod or a similar structured type library
Beta Was this translation helpful? Give feedback.
All reactions