-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
docs: add testing with storybook #16701
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
Merged
dummdidumm
merged 7 commits into
sveltejs:main
from
kylegach:svelte-docs-testing-with-storybook
Sep 4, 2025
+44
−2
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e7ccf7
docs: add testing with storybook
kylegach 7b3bfaa
docs: remove video
kylegach 805d454
docs: address feedback
kylegach b78774b
Revise Storybook testing documentation
dummdidumm 3966b34
Update documentation/docs/07-misc/02-testing.md
dummdidumm dd45729
Tweaks
kylegach 908e35b
make it its own section
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,9 +160,11 @@ export function logger(getValue) { | |
|
||
### Component testing | ||
|
||
It is possible to test your components in isolation using Vitest. | ||
It is possible to test your components in isolation, which allows you to render them in a browser (real or simulated), simulate behavior, and make assertions, without spinning up your whole app. | ||
|
||
> [!NOTE] Before writing component tests, think about whether you actually need to test the component, or if it's more about the logic _inside_ the component. If so, consider extracting out that logic to test it in isolation, without the overhead of a component | ||
> [!NOTE] Before writing component tests, think about whether you actually need to test the component, or if it's more about the logic _inside_ the component. If so, consider extracting out that logic to test it in isolation, without the overhead of a component. | ||
|
||
_With Vitest and jsdom_ | ||
|
||
To get started, install jsdom (a library that shims DOM APIs): | ||
|
||
|
@@ -246,6 +248,50 @@ test('Component', async () => { | |
|
||
When writing component tests that involve two-way bindings, context or snippet props, it's best to create a wrapper component for your specific test and interact with that. `@testing-library/svelte` contains some [examples](https://testing-library.com/docs/svelte-testing-library/example). | ||
|
||
_With Storybook_ | ||
|
||
[Storybook](https://storybook.js.org) is a tool for developing and documenting UI components, and it can also be used to test your components. They're run with Vitest's browser mode, which renders your components in a real browser for the most realistic testing environment. | ||
|
||
To get started, first install Storybook ([using Svelte's CLI](/docs/cli/storybook)) in your project via `npx sv add storybook` and choose the recommended configuration that includes testing features. Else follow the [Storybooks docs](https://storybook.js.org/docs/get-started/frameworks/svelte-vite#getting-started) on getting started. | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
You can then create stories for component variations and test interactions with the [play function](https://storybook.js.org/docs/writing-tests/interaction-testing?renderer=svelte#writing-interaction-tests), which allows you to simulate behavior and make assertions using the Testing Library and Vitest APIs. Here's an example of two stories that can be tested, one that renders an empty Form component and one that simulates a user filling out the form: | ||
|
||
```svelte | ||
/// file: LoginForm.stories.svelte | ||
<script module> | ||
import { defineMeta } from '@storybook/addon-svelte-csf'; | ||
import { expect, fn } from 'storybook/test'; | ||
|
||
import LoginForm from './LoginForm.svelte'; | ||
|
||
const { Story } = defineMeta({ | ||
component: LoginForm, | ||
args: { | ||
// Pass a mock function to the `onSubmit` prop | ||
onSubmit: fn(), | ||
} | ||
}); | ||
</script> | ||
|
||
<Story name="Empty Form" /> | ||
|
||
<Story | ||
name="Filled Form" | ||
play={async ({ args, canvas, userEvent }) => { | ||
// Simulate a user filling out the form | ||
await userEvent.type(canvas.getByTestId('email'), '[email protected]'); | ||
await userEvent.type(canvas.getByTestId('password'), 'a-random-password'); | ||
await userEvent.click(canvas.getByRole('button')); | ||
|
||
// Run assertions | ||
await expect(args.onSubmit).toHaveBeenCalledTimes(1); | ||
await expect(canvas.getByText('You’re in!')).toBeInTheDocument(); | ||
}} | ||
/> | ||
``` | ||
|
||
To learn more about Storybook's mocking, accessibility testing, interactions debugging, and coverage tools, please see the [Storybook testing docs](https://storybook.js.org/docs/writing-tests?renderer=svelte). | ||
|
||
## E2E tests using Playwright | ||
|
||
E2E (short for 'end to end') tests allow you to test your full application through the eyes of the user. This section uses [Playwright](https://playwright.dev/) as an example, but you can also use other solutions like [Cypress](https://www.cypress.io/) or [NightwatchJS](https://nightwatchjs.org/). | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to make this a heading, but the level required here,
h4
, is not styled in the docs site.If you agree that the
h4
is necessary, I'll need some help navigating where to place that style. 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternative would be to move testing out of misc and make it a separate top level category so h3 should work here. This would be a larger change though so waiting for feedback from other svelte team members.