Skip to content

Commit 6e7ccf7

Browse files
committed
docs: add testing with storybook
1 parent 548ad7b commit 6e7ccf7

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

documentation/docs/07-misc/02-testing.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ export function logger(getValue) {
160160

161161
### Component testing
162162

163-
It is possible to test your components in isolation using Vitest.
163+
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.
164164

165-
> [!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
165+
> [!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.
166+
167+
_With Vitest and jsdom_
166168

167169
To get started, install jsdom (a library that shims DOM APIs):
168170

@@ -246,6 +248,89 @@ test('Component', async () => {
246248

247249
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).
248250

251+
_With Storybook_
252+
253+
[Storybook](https://storybook.js.org?ref=svelte-docs) is the industry standard for developing and documenting UI components, and it can also be used to test your components. A storybook is made up of [stories](https://storybook.js.org/docs/writing-stories?ref=svelte-docs&renderer=svelte)—isolated examples of your components in different states and with simulated interactions. These stories can also function as [component tests](https://storybook.js.org/docs/writing-tests?ref=svelte-docs&renderer=svelte), with no additional code needed. They're run with Vitest's browser mode, which renders your components in a real browser for the most realistic testing environment.
254+
255+
To get started, first install Storybook ([using Svelte's CLI](/docs/cli/storybook)) in your project if you haven't already:
256+
257+
```sh
258+
npx sv add storybook
259+
```
260+
261+
When prompted, choose the recommended configuration that includes testing features.
262+
263+
If you already have Storybook set up, but are not yet using the testing features, first upgrade to the latest version:
264+
265+
```sh
266+
npx storybook@latest upgrade
267+
```
268+
269+
Then, install and configure Storybook's Vitest addon:
270+
271+
```sh
272+
npx storybook add @storybook/addon-vitest
273+
```
274+
275+
That `add` command will install and register the Vitest addon. It will also inspect your project's Vite and Vitest setup, and install and configure them with sensible defaults, if necessary. You may need to adjust the [configuration](https://storybook.js.org/docs/writing-tests/integrations/vitest-addon?ref=svelte-docs&renderer=svelte#options) to fit your project's needs.
276+
277+
When you run Storybook, you will see a test widget in the bottom of your sidebar, from where you can run your tests. Each story is automatically transformed into a test. The results will be shown in the sidebar and you can debug your tests using Storybook's tools and the browser devtools. You can also integrate [accessibility tests](https://storybook.js.org/docs/writing-tests/accessibility?ref=svelte-docs&renderer=svelte), which will run alongside your component tests.
278+
279+
<figure>
280+
<video autoplay loop muted style="max-width: 100%; height: auto;">
281+
<source type="video/mp4" src="/media/storybook-test-overview.mp4">
282+
</video>
283+
<figcaption>
284+
Running component tests in Storybook, filtering the sidebar to test failures, turning on watch mode, and using the interactions debugger to fix the failure
285+
</figcaption>
286+
</figure>
287+
288+
You can also run those component tests in the terminal (and in CI) using the Vitest CLI.:
289+
290+
```sh
291+
npx vitest --project storybook
292+
```
293+
294+
You can create stories for component variations and test interactions with the [play function](https://storybook.js.org/docs/writing-tests/interaction-testing?ref=svelte-docs&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:
295+
296+
```svelte
297+
/// file: LoginForm.stories.svelte
298+
<script module>
299+
import { defineMeta } from '@storybook/addon-svelte-csf';
300+
import { expect, fn } from 'storybook/test';
301+
302+
import LoginForm from './LoginForm.svelte';
303+
304+
const { Story } = defineMeta({
305+
component: LoginForm,
306+
args: {
307+
// 👇 Pass a mock function to the `onSubmit` prop
308+
onSubmit: fn(),
309+
}
310+
});
311+
</script>
312+
313+
<Story name="Empty Form" />
314+
315+
<Story
316+
name="Filled Form"
317+
play={async ({ args, canvas, userEvent }) => {
318+
// 👇 Simulate a user filling out the form
319+
await userEvent.type(canvas.getByTestId('email'), 'email@provider.com');
320+
await userEvent.type(canvas.getByTestId('password'), 'a-random-password');
321+
await userEvent.click(canvas.getByRole('button'));
322+
323+
// 👇 Assert that the `onSubmit` function was called
324+
await expect(args.onSubmit).toHaveBeenCalledTimes(1);
325+
326+
// 👇 Assert that the success message is shown
327+
await expect(canvas.getByText('You’re in!')).toBeInTheDocument();
328+
}}
329+
/>
330+
```
331+
332+
When you view that story or run that test in Storybook, you can see each step of the simulated behavior and the assertions made against the component. If anything goes wrong, you can step back-and-forth through the test to pinpoint exactly where the issue occurred.
333+
249334
## E2E tests using Playwright
250335

251336
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/).

0 commit comments

Comments
 (0)