Skip to content

Commit b2fc58d

Browse files
committed
docs: add code of files being tested
closes #14900
1 parent 7737868 commit b2fc58d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ test('Multiplier', () => {
5353
});
5454
```
5555

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

5878
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.
@@ -75,6 +95,21 @@ test('Multiplier', () => {
7595
});
7696
```
7797

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

80115
```js
@@ -105,6 +140,27 @@ test('Effect', () => {
105140
});
106141
```
107142

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

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

0 commit comments

Comments
 (0)