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
Copy file name to clipboardExpand all lines: documentation/docs/07-misc/02-testing.md
+87-2Lines changed: 87 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,9 +160,11 @@ export function logger(getValue) {
160
160
161
161
### Component testing
162
162
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.
164
164
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_
166
168
167
169
To get started, install jsdom (a library that shims DOM APIs):
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).
248
250
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.
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';
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
+
249
334
## E2E tests using Playwright
250
335
251
336
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