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
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`:
59
80
60
81
```js
61
82
/// file: multiplier.svelte.test.js
@@ -75,6 +96,21 @@ test('Multiplier', () => {
75
96
});
76
97
```
77
98
99
+
```js
100
+
/// file: multiplier.svelte.js
101
+
/**
102
+
* @param{() => number}getCount
103
+
* @param{number}k
104
+
*/
105
+
exportfunctionmultiplier(getCount, k) {
106
+
return {
107
+
getvalue() {
108
+
returngetCount() * k;
109
+
}
110
+
};
111
+
}
112
+
```
113
+
78
114
If the code being tested uses effects, you need to wrap the test inside `$effect.root`:
79
115
80
116
```js
@@ -105,6 +141,27 @@ test('Effect', () => {
105
141
});
106
142
```
107
143
144
+
```js
145
+
/// file: logger.svelte.js
146
+
/**
147
+
* @param{() => any}getValue
148
+
*/
149
+
exportfunctionlogger(getValue) {
150
+
/**@type{any[]}*/
151
+
let log =$state([]);
152
+
153
+
$effect(() => {
154
+
log.push(getValue());
155
+
});
156
+
157
+
return {
158
+
getvalue() {
159
+
return log;
160
+
}
161
+
};
162
+
}
163
+
```
164
+
108
165
### Component testing
109
166
110
167
It is possible to test your components in isolation using Vitest.
0 commit comments