You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use Vitest to provide component code coverage stats in realtime in Storybook's UI
Problem Statement
Code coverage shows how a test suite exercises the lines/branches of code. It's typically accessible via a report, showing the overall "test health" of your project. But when developing a component in isolation, it's helpful to know its code coverage so you can create tests with a rapid feedback loop. This proposal aims to provide realtime code coverage in Storybook's UI that updates as you modify the component or its stories.
Non-goals
Full project coverage is also helpful but challenging to compute in realtime for large projects. Producing a standards-compliant project coverage report in a test run is also a useful project, but it is a separate one.
Implementation
I propose an addon that shows a component's coverage when that component is selected in Storybook's UI:
When the component or its stories are updated, that coverage should also update in real time. This can be achieved if we define "component coverage" as the coverage that comes explicitly from the stories associated with that component. Consider a component Button with stories defined in Button.stories.tsx. Updating Button's component coverage would consist of running all the stories in Button.stories.tsx through a coverage tool like Vitest. This approximation would ignore coverage from stories for higher-level components that include Button. But it's an approximation that should be computable almost instantly.
In addition to displaying coverage as a single metric in the tab, the addon should also show the spans of Component code that are covered by those tests:
We can implement all this on top of the Vitest integration by selectively calling Vite and using its Node API on specific story files.
Prior Art
Storybook has an existing coverage addon. It focuses on project coverage, does not update in real time, and requires some configuration. However, it could (and probably should!) be the basis for this project.
In addition, many code coverage tools are out there with different visualizations. Some notable ones:
Addon that displays component coverage in its addon tab and annotated component source in its addon panel.
Mechanism for watching files and invoking Vitest on a story file when the file or its component is edited. This might be part of Vitest's watch mode or a separate mechanism
Mechanism for reporting the results from Vitest back to the addon
Risks
This could result in Storybook using more computing resources and lead to a bad experience for users on slow machines
Depending on the detailed implementation, this could conflict with Vitest-based functional testing
The "component coverage" approximation is not useful. Users feel that they need coverage from higher-level components.
Unresolved Questions
Is this compact with the Vitest browser and mode, or do we need to use happy-dom? If the latter, does it open us up to many additional corner cases?
Can we use Vitest watch mode, or do we need a separate mechanism to watch for file changes and invoke Vitest separately?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Summary
Use Vitest to provide component code coverage stats in realtime in Storybook's UI
Problem Statement
Code coverage shows how a test suite exercises the lines/branches of code. It's typically accessible via a report, showing the overall "test health" of your project. But when developing a component in isolation, it's helpful to know its code coverage so you can create tests with a rapid feedback loop. This proposal aims to provide realtime code coverage in Storybook's UI that updates as you modify the component or its stories.
Non-goals
Full project coverage is also helpful but challenging to compute in realtime for large projects. Producing a standards-compliant project coverage report in a test run is also a useful project, but it is a separate one.
Implementation
I propose an addon that shows a component's coverage when that component is selected in Storybook's UI:
When the component or its stories are updated, that coverage should also update in real time. This can be achieved if we define "component coverage" as the coverage that comes explicitly from the stories associated with that component. Consider a component
Button
with stories defined inButton.stories.tsx
. UpdatingButton
's component coverage would consist of running all the stories inButton.stories.tsx
through a coverage tool like Vitest. This approximation would ignore coverage from stories for higher-level components that includeButton
. But it's an approximation that should be computable almost instantly.In addition to displaying coverage as a single metric in the tab, the addon should also show the spans of Component code that are covered by those tests:
We can implement all this on top of the Vitest integration by selectively calling Vite and using its Node API on specific story files.
Prior Art
Storybook has an existing coverage addon. It focuses on project coverage, does not update in real time, and requires some configuration. However, it could (and probably should!) be the basis for this project.
In addition, many code coverage tools are out there with different visualizations. Some notable ones:
Deliverables
Risks
Unresolved Questions
happy-dom
? If the latter, does it open us up to many additional corner cases?Alternatives considered / Abandoned Ideas
No response
Beta Was this translation helpful? Give feedback.
All reactions