Skip to content

Commit 82c00b9

Browse files
authored
fix testing page by installing libraries needed for typechecking (#243)
1 parent c028886 commit 82c00b9

File tree

3 files changed

+461
-2
lines changed

3 files changed

+461
-2
lines changed

apps/svelte.dev/content/docs/svelte/05-misc/02-testing.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ export default defineConfig({ /* ... */ })
2727
You can now write unit tests for code inside your `.js/.ts` files:
2828

2929
```js
30+
// @filename: multiplier.d.ts
31+
export declare function multiplier(a: number, b: number): {
32+
value: number;
33+
set(value: number): void;
34+
};
35+
36+
// @filename: index.js
37+
// ---cut---
3038
/// file: multiplier.svelte.test.js
3139
import { flushSync } from 'svelte';
3240
import { expect, test } from 'vitest';
@@ -48,6 +56,11 @@ test('Multiplier', () => {
4856
It is possible to use runes inside your test files. First ensure your bundler knows to route the file through the Svelte compiler before running the test by adding `.svelte` to the filename (e.g `multiplier.svelte.test.js`). After that, you can use runes inside your tests.
4957

5058
```js
59+
// @filename: multiplier.svelte.d.ts
60+
export declare function multiplier(fn: () => number, b: number): { value: number };
61+
62+
// @filename: index.js
63+
// ---cut---
5164
/// file: multiplier.svelte.test.js
5265
import { flushSync } from 'svelte';
5366
import { expect, test } from 'vitest';
@@ -68,6 +81,11 @@ test('Multiplier', () => {
6881
If the code being tested uses effects, you need to wrap the test inside `$effect.root`:
6982

7083
```js
84+
// @filename: logger.svelte.d.ts
85+
export declare function logger(fn: () => any): { value: number };
86+
87+
// @filename: index.js
88+
// ---cut---
7189
/// file: logger.svelte.test.js
7290
import { flushSync } from 'svelte';
7391
import { expect, test } from 'vitest';
@@ -135,6 +153,7 @@ export default defineConfig({
135153
After that, you can create a test file in which you import the component to test, interact with it programmatically and write expectations about the results:
136154

137155
```js
156+
// ---cut---
138157
/// file: component.test.js
139158
import { flushSync, mount, unmount } from 'svelte';
140159
import { expect, test } from 'vitest';
@@ -150,7 +169,7 @@ test('Component', () => {
150169
expect(document.body.innerHTML).toBe('<button>0</button>');
151170

152171
// Click the button, then flush the changes so you can synchronously write expectations
153-
document.body.querySelector('button').click();
172+
document.body.querySelector('button')?.click();
154173
flushSync();
155174

156175
expect(document.body.innerHTML).toBe('<button>1</button>');
@@ -164,7 +183,8 @@ While the process is very straightforward, it is also low level and somewhat bri
164183
165184
```js
166185
/// file: component.test.js
167-
import { render, screen } from '@testing-library/svelte';
186+
import { render } from '@testing-library/svelte';
187+
import { screen } from '@testing-library/dom';
168188
import userEvent from '@testing-library/user-event';
169189
import { expect, test } from 'vitest';
170190
import Component from './Component.svelte';
@@ -208,6 +228,7 @@ export default config;
208228
You can now start writing tests. These are totally unaware of Svelte as a framework, so you mainly interact with the DOM and write assertions.
209229
210230
```js
231+
// @errors: 2307 7031
211232
/// file: tests/hello-world.spec.js
212233
import { expect, test } from '@playwright/test';
213234

apps/svelte.dev/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
"@replit/codemirror-vim": "^6.0.14",
3636
"@rich_harris/svelte-split-pane": "^1.1.3",
3737
"@sveltejs/repl": "workspace:*",
38+
"@testing-library/dom": "^10.4.0",
39+
"@testing-library/svelte": "^5.2.3",
40+
"@testing-library/user-event": "^14.5.2",
3841
"@types/d3-geo": "^3.1.0",
3942
"@vercel/speed-insights": "^1.0.0",
4043
"@webcontainer/api": "^1.1.5",
@@ -53,6 +56,7 @@
5356
"prism-svelte": "^0.5.0",
5457
"prismjs": "^1.29.0",
5558
"topojson-client": "^3.1.0",
59+
"vitest": "^2.1.2",
5660
"ws": "^8.13.0",
5761
"yootils": "^0.3.1"
5862
},

0 commit comments

Comments
 (0)