Replies: 2 comments 1 reply
-
I'm very interested in using this plugin. Is it still in progress? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Thanks, I appreciate it!
…________________________________
From: Michael Shilman ***@***.***>
Sent: Monday, September 30, 2024 11:15 PM
To: storybookjs/storybook ***@***.***>
Cc: Heather Fryling ***@***.***>; Comment ***@***.***>
Subject: Re: [storybookjs/storybook] [RFC] Storybook Vitest integration (Discussion #28386)
This was released in Storybook 8.3<https://storybook.js.org/blog/storybook-8-3/>. Give it a try!
—
Reply to this email directly, view it on GitHub<#28386 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BJZDBUAVQ3FGBAIGX2MGKHDZZI4Y7AVCNFSM6AAAAABKCAHXJOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAOBQGUYDGOI>.
You are receiving this because you commented.Message ID: ***@***.***>
|
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
-
RFC: Testing stories with Vitest via a plugin
Status: Draft
1. Summary
This RFC proposes a new method for testing stories in Storybook using a Vitest plugin. The plugin will transform story files into test files using the portable stories API, providing a seamless integration with Vitest which will improve the developer experience for Vite users.
As a result, you get:
2. The Problem
The current Storybook test-runner is based on a Jest + Playwright integration, which, while effective, may not align with the development tools and workflows used by all teams. It depends on
jest-playwright
, a package that has been deprecated since 2021 and is no longer maintained.The Vite ecosystem has grown a lot since the Storybook test-runner was released, and nowadays many Storybook projects are Vite based. For these projects, there are common requests/complaints from users:
jest-image-snapshot
,jest-junit
and others for additional features instead of using solutions they might already have for Vitest.3. Non-goals
4. Proposed Solution
The proposed solution involves creating a Vitest plugin that leverages the portable stories API to transform story files into test files.
Registering the plugin
You can choose one of the two possible ways:
This will probably not be the most recommended way, because you might want to test your stories slightly differently than your unit tests, for instance test stories in Browser mode while running unit tests in Node.
You could also set it up in a separate config file e.g.
vitest.storybook.config.ts
which is passed on your test command e.g.vitest -c vitest.storybook.config.ts
. However if you are planning on creating separate test environments, you should instead use Vitest workspaces.This will create a Vitest project specific to the Storybook plugin, which can be configured separately and won't impact other test configuration.
As a result, every story you have in your codebase is turned into tests automatically, which will be tested as part of your existing test command.
5. Implementation
Vitest plugin: Prerequisites
In order to use the plugin, we will assume that your project matches the following requisites:
prelude: Portable stories API
Before talking about the implementation, it's important to understand about the portable stories API.
Users can use this API to reuse stories in separate environments, such as unit tests. To set it up, users have to follow three steps:
By running the
Button
tests, this API allows for:This essentially recreates the behavior of Storybook (of preparing and testing the story), however in an external environment, without having to run Storybook.
Vitest plugin: Implementation
The Vitest plugin will leverage the portable stories API and essentially provide all the steps (described above) for setting it up, without you having to do any of them yourself. It will either receive the renderer (e.g.
react
,vue3
) as an option, or it will try to figure it out based on your Storybook config file.1. Virtual file for setting project annotations and hooks
The plugin will create and register virtual setup file that includes all the boilerplate mentioned previously, which will only exist virtually during the test run.
2. Composing stories in a test file
The plugin will provide all the boilerplate for composing stories without having to write the test file yourself.
Here is an example transformation, based on the following story file:
The Vitest plugin will transform it into:
3. Spawning Storybook in background + "click to debug" experience
When running tests in watch mode, the plugin should spawn Storybook. The idea is to provide a "click to debug" experience where if your tests fail, the error message should include something like:
To make it simpler, the plugin could receive a command to execute instead of having to figure out how to run Storybook in the user's codebase (as it could be highly customized):
Vitest reporters can be used to implement this behavior.
4. Automatic viewport dimension
If users are using
@storybook/addon-viewport
and running Vitest in Browser mode, the plugin should leverage the page object from the Browser context to set up the viewports correctly. This is necessary for tests to pass if they assert behavior that is container size dependenent.5. Tags filtering
The plugin should support tags filtering via options, the same way the test-runner supports, e.g.:
it could use Vitest's skipIf and runIf APIs for that.
6. Prior Art
7. Deliverables
8. Risks
9. Unresolved Questions
Can we extend support to other frameworks or bundlers in the future?
Definitely, as long as we improve the portable stories API, we can broaden the support in the Vitest plugin. For non-Vite based projects that will not be the case, however.
What additional features or improvements can be made to the portable stories API to enhance this integration?
A big amount of improvements is being done in Storybook 8.2 which will make it easier to support more frameworks in the future.
10. Alternatives considered / Abandoned Ideas
jest-playwright-preset
being unmaintained).jest-playwright-preset
package. This means quite some effort because it should still maintain all the functionality of the existing implementation, and we would like to experiment with an innovative solution like the Vitest integration for the time being.Beta Was this translation helpful? Give feedback.
All reactions