Skip to content

Commit b78774b

Browse files
authored
Revise Storybook testing documentation
condense, remove referer query parameters
1 parent 805d454 commit b78774b

File tree

1 file changed

+7
-39
lines changed

1 file changed

+7
-39
lines changed

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

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,39 +250,11 @@ When writing component tests that involve two-way bindings, context or snippet p
250250

251251
_With Storybook_
252252

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.
253+
[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.
254254

255-
To get started, first install Storybook ([using Svelte's CLI](/docs/cli/storybook)) in your project if you haven't already:
255+
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.
256256

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-
You can also run those component tests in the terminal (and in CI) using the Vitest CLI.:
280-
281-
```sh
282-
npx vitest --project storybook
283-
```
284-
285-
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:
257+
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:
286258

287259
```svelte
288260
/// file: LoginForm.stories.svelte
@@ -295,7 +267,7 @@ You can create stories for component variations and test interactions with the [
295267
const { Story } = defineMeta({
296268
component: LoginForm,
297269
args: {
298-
// 👇 Pass a mock function to the `onSubmit` prop
270+
// Pass a mock function to the `onSubmit` prop
299271
onSubmit: fn(),
300272
}
301273
});
@@ -306,23 +278,19 @@ You can create stories for component variations and test interactions with the [
306278
<Story
307279
name="Filled Form"
308280
play={async ({ args, canvas, userEvent }) => {
309-
// 👇 Simulate a user filling out the form
281+
// Simulate a user filling out the form
310282
await userEvent.type(canvas.getByTestId('email'), '[email protected]');
311283
await userEvent.type(canvas.getByTestId('password'), 'a-random-password');
312284
await userEvent.click(canvas.getByRole('button'));
313285
314-
// 👇 Assert that the `onSubmit` function was called
286+
// Run assertions
315287
await expect(args.onSubmit).toHaveBeenCalledTimes(1);
316-
317-
// 👇 Assert that the success message is shown
318288
await expect(canvas.getByText('You’re in!')).toBeInTheDocument();
319289
}}
320290
/>
321291
```
322292

323-
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.
324-
325-
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?ref=svelte-docs&renderer=svelte).
293+
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).
326294

327295
## E2E tests using Playwright
328296

0 commit comments

Comments
 (0)