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
+44-2Lines changed: 44 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,9 +160,9 @@ 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
166
167
167
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
248
249
+
### Component testing with Storybook
250
+
251
+
[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.
252
+
253
+
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. If you're already using Storybook, and for more information on Storybook's testing capabilities, follow the [Storybook testing docs](https://storybook.js.org/docs/writing-tests?renderer=svelte) to get started.
254
+
255
+
You can 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 LoginForm component and one that simulates a user filling out the form:
256
+
257
+
```svelte
258
+
/// file: LoginForm.stories.svelte
259
+
<script module>
260
+
import { defineMeta } from '@storybook/addon-svelte-csf';
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/).
Copy file name to clipboardExpand all lines: documentation/docs/07-misc/04-custom-elements.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Custom elements
4
4
5
5
<!-- - [basically what we have today](https://svelte.dev/docs/custom-elements-api) -->
6
6
7
-
Svelte components can also be compiled to custom elements (aka web components) using the `customElement: true` compiler option. You should specify a tag name for the component using the `<svelte:options>`[element](svelte-options).
7
+
Svelte components can also be compiled to custom elements (aka web components) using the `customElement: true` compiler option. You should specify a tag name for the component using the `<svelte:options>`[element](svelte-options). Within the custom element you can access the host element via the [`$host`](https://svelte.dev/docs/svelte/$host) rune.
`{@const}` must be the immediate child of `{#snippet}`, `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `<svelte:fragment>`, `<svelte:boundary` or `<Component>`
197
197
```
198
198
199
+
### const_tag_invalid_reference
200
+
201
+
```
202
+
The `{@const %name% = ...}` declaration is not available in this snippet
203
+
```
204
+
205
+
The following is an error:
206
+
207
+
```svelte
208
+
<svelte:boundary>
209
+
{@const foo = 'bar'}
210
+
211
+
{#snippet failed()}
212
+
{foo}
213
+
{/snippet}
214
+
</svelte:boundary>
215
+
```
216
+
217
+
Here, `foo` is not available inside `failed`. The top level code inside `<svelte:boundary>` becomes part of the implicit `children` snippet, in other words the above code is equivalent to this:
Copy file name to clipboardExpand all lines: packages/svelte/messages/compile-errors/template.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,6 +124,49 @@
124
124
125
125
> `{@const}` must be the immediate child of `{#snippet}`, `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `<svelte:fragment>`, `<svelte:boundary` or `<Component>`
126
126
127
+
## const_tag_invalid_reference
128
+
129
+
> The `{@const %name% = ...}` declaration is not available in this snippet
130
+
131
+
The following is an error:
132
+
133
+
```svelte
134
+
<svelte:boundary>
135
+
{@const foo = 'bar'}
136
+
137
+
{#snippet failed()}
138
+
{foo}
139
+
{/snippet}
140
+
</svelte:boundary>
141
+
```
142
+
143
+
Here, `foo` is not available inside `failed`. The top level code inside `<svelte:boundary>` becomes part of the implicit `children` snippet, in other words the above code is equivalent to this:
144
+
145
+
```svelte
146
+
<svelte:boundary>
147
+
{#snippet children()}
148
+
{@const foo = 'bar'}
149
+
{/snippet}
150
+
151
+
{#snippet failed()}
152
+
{foo}
153
+
{/snippet}
154
+
</svelte:boundary>
155
+
```
156
+
157
+
The same applies to components:
158
+
159
+
```svelte
160
+
<Component>
161
+
{@const foo = 'bar'}
162
+
163
+
{#snippet someProp()}
164
+
<!-- error -->
165
+
{foo}
166
+
{/snippet}
167
+
</Component>
168
+
```
169
+
127
170
## debug_tag_invalid_arguments
128
171
129
172
> {@debug ...} arguments must be identifiers, not arbitrary expressions
Copy file name to clipboardExpand all lines: packages/svelte/src/compiler/errors.js
+10Lines changed: 10 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -985,6 +985,16 @@ export function const_tag_invalid_placement(node) {
985
985
e(node,'const_tag_invalid_placement',`\`{@const}\` must be the immediate child of \`{#snippet}\`, \`{#if}\`, \`{:else if}\`, \`{:else}\`, \`{#each}\`, \`{:then}\`, \`{:catch}\`, \`<svelte:fragment>\`, \`<svelte:boundary\` or \`<Component>\`\nhttps://svelte.dev/e/const_tag_invalid_placement`);
986
986
}
987
987
988
+
/**
989
+
* The `{@const %name% = ...}` declaration is not available in this snippet
e(node,'const_tag_invalid_reference',`The \`{@const ${name} = ...}\` declaration is not available in this snippet \nhttps://svelte.dev/e/const_tag_invalid_reference`);
996
+
}
997
+
988
998
/**
989
999
* {@debug ...} arguments must be identifiers, not arbitrary expressions
0 commit comments