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
+56Lines changed: 56 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,6 +53,26 @@ test('Multiplier', () => {
53
53
});
54
54
```
55
55
56
+
```js
57
+
/// file: multiplier.js
58
+
/**
59
+
* @param{number}initial
60
+
* @param{number}mult
61
+
*/
62
+
exportfunctionmultiplier(initial, mult) {
63
+
let count = initial;
64
+
65
+
return {
66
+
getvalue() {
67
+
return count * mult;
68
+
},
69
+
set: (c) => {
70
+
count = c;
71
+
}
72
+
};
73
+
}
74
+
```
75
+
56
76
### Using runes inside your test files
57
77
58
78
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', () => {
75
95
});
76
96
```
77
97
98
+
```js
99
+
/// file: multiplier.svelte.js
100
+
/**
101
+
* @param{() => number}getCount
102
+
* @param{number}mult
103
+
*/
104
+
exportfunctionmultiplier(getCount, mult) {
105
+
return {
106
+
getvalue() {
107
+
returngetCount() * mult;
108
+
}
109
+
};
110
+
}
111
+
```
112
+
78
113
If the code being tested uses effects, you need to wrap the test inside `$effect.root`:
79
114
80
115
```js
@@ -105,6 +140,27 @@ test('Effect', () => {
105
140
});
106
141
```
107
142
143
+
```js
144
+
/// file: logger.svelte.js
145
+
/**
146
+
* @param{() => any}getValue
147
+
*/
148
+
exportfunctionlogger(getValue) {
149
+
/**@type{any[]}*/
150
+
let log =$state([]);
151
+
152
+
$effect(() => {
153
+
log.push(getValue());
154
+
});
155
+
156
+
return {
157
+
getvalue() {
158
+
return log;
159
+
}
160
+
};
161
+
}
162
+
```
163
+
108
164
### Component testing
109
165
110
166
It is possible to test your components in isolation using Vitest.
0 commit comments