Skip to content
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

[Bug]: useArgs from Preview API has no effect when running Vitest addon from CLI #30207

Closed
adriantrunzo opened this issue Jan 7, 2025 · 1 comment

Comments

@adriantrunzo
Copy link

adriantrunzo commented Jan 7, 2025

Describe the bug

This bug applies to the experimental Vitest Test Addon.

When testing (i.e play function) a controlled component that is connected to the Story args via useArgs, the tests pass when run within the Storybook UI, but fail when run from the Vitest CLI. I don't expect the tests to behave differently depending on how they are run.

Deconstructing setArgs from useArgs and calling it in a change handler is a common approach for creating Stories for controlled components. The issue occurs when setArgs is called during the execution of the play function. For example, when typing into an input whose change handler calls setArgs. In the Storybook UI, setArgs works as expected and the story re-renders with the new state. In the Vitest CLI, however, setArgs seems to have no effect and the story does not re-render.

In the reproduction I created, the attributes of an input element change depending on the args. The play function asserts the value of an attribute. This example is contrived, but mimics the actual use case I have in which the component renders different elements based on the args.

Reproduction link

https://github.com/adriantrunzo/sb-useargs-test

Reproduction steps

  1. Clone the repository linked above
  2. pnpm install
  3. Run storybook with pnpm storybook
  4. Navigate to the test story and verify that the component tests pass
  5. Run the tests from CLI with pnpm vitest and verify that they fail

System

Storybook Environment Info:

  System:
    OS: macOS 15.1.1
    CPU: (12) arm64 Apple M2 Max
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.local/state/fnm_multishells/26912_1736259643222/bin/node
    npm: 10.9.0 - ~/.local/state/fnm_multishells/26912_1736259643222/bin/npm
    pnpm: 9.14.4 - ~/.local/state/fnm_multishells/26912_1736259643222/bin/pnpm <----- active
  Browsers:
    Chrome: 131.0.6778.205
    Safari: 18.1.1
  npmPackages:
    @storybook/addon-essentials: ^8.4.7 => 8.4.7
    @storybook/addon-onboarding: ^8.4.7 => 8.4.7
    @storybook/blocks: ^8.4.7 => 8.4.7
    @storybook/experimental-addon-test: ^8.4.7 => 8.4.7
    @storybook/preview-api: ^8.4.7 => 8.4.7
    @storybook/react: ^8.4.7 => 8.4.7
    @storybook/react-vite: ^8.4.7 => 8.4.7
    @storybook/test: ^8.4.7 => 8.4.7
    eslint-plugin-storybook: ^0.11.2 => 0.11.2
    storybook: ^8.4.7 => 8.4.7

Additional context

No response

@adriantrunzo
Copy link
Author

Duplicate of #29774.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant