Skip to content

Commit a1f371e

Browse files
docs: add code of files being tested (#14925)
* docs: add code of files being tested closes #14900 * fix * Apply suggestions from code review Co-authored-by: Rich Harris <[email protected]> * Update documentation/docs/07-misc/02-testing.md Co-authored-by: Rich Harris <[email protected]> * Apply suggestions from code review Co-authored-by: Rich Harris <[email protected]> * rename import * from sveltejs/svelte.dev#1094 --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 7737868 commit a1f371e

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

documentation/docs/02-runes/02-$state.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ todos[0].done = !todos[0].done;
4444
If you push a new object to the array, it will also be proxified:
4545

4646
```js
47-
// @filename: ambient.d.ts
48-
declare global {
49-
const todos: Array<{ done: boolean, text: string }>
50-
}
51-
52-
// @filename: index.js
47+
let todos = [{ done: false, text: 'add more todos' }];
5348
// ---cut---
5449
todos.push({
5550
done: false,

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ You can now write unit tests for code inside your `.js/.ts` files:
4040
/// file: multiplier.svelte.test.js
4141
import { flushSync } from 'svelte';
4242
import { expect, test } from 'vitest';
43-
import { multiplier } from './multiplier.js';
43+
import { multiplier } from './multiplier.svelte.js';
4444

4545
test('Multiplier', () => {
4646
let double = multiplier(0, 2);
@@ -53,9 +53,30 @@ test('Multiplier', () => {
5353
});
5454
```
5555

56+
```js
57+
/// file: multiplier.svelte.js
58+
/**
59+
* @param {number} initial
60+
* @param {number} k
61+
*/
62+
export function multiplier(initial, k) {
63+
let count = $state(initial);
64+
65+
return {
66+
get value() {
67+
return count * k;
68+
},
69+
/** @param {number} c */
70+
set: (c) => {
71+
count = c;
72+
}
73+
};
74+
}
75+
```
76+
5677
### Using runes inside your test files
5778

58-
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.
79+
Since Vitest processes your test files the same way as your source files, you can use runes inside your tests as long as the filename includes `.svelte`:
5980

6081
```js
6182
/// file: multiplier.svelte.test.js
@@ -75,6 +96,21 @@ test('Multiplier', () => {
7596
});
7697
```
7798

99+
```js
100+
/// file: multiplier.svelte.js
101+
/**
102+
* @param {() => number} getCount
103+
* @param {number} k
104+
*/
105+
export function multiplier(getCount, k) {
106+
return {
107+
get value() {
108+
return getCount() * k;
109+
}
110+
};
111+
}
112+
```
113+
78114
If the code being tested uses effects, you need to wrap the test inside `$effect.root`:
79115

80116
```js
@@ -105,6 +141,27 @@ test('Effect', () => {
105141
});
106142
```
107143

144+
```js
145+
/// file: logger.svelte.js
146+
/**
147+
* @param {() => any} getValue
148+
*/
149+
export function logger(getValue) {
150+
/** @type {any[]} */
151+
let log = $state([]);
152+
153+
$effect(() => {
154+
log.push(getValue());
155+
});
156+
157+
return {
158+
get value() {
159+
return log;
160+
}
161+
};
162+
}
163+
```
164+
108165
### Component testing
109166

110167
It is possible to test your components in isolation using Vitest.

0 commit comments

Comments
 (0)