@@ -5,6 +5,7 @@ sidebar_label: FAQ
5
5
---
6
6
7
7
- [ Does this library also work with SvelteKit?] ( #does-this-library-also-work-with-sveltekit )
8
+ - [ ` onMount ` isn't called when rendering components] ( #onmount-isnt-called-when-rendering-compoments )
8
9
- [ Testing file upload component] ( #testing-file-upload-component )
9
10
10
11
---
@@ -14,6 +15,37 @@ sidebar_label: FAQ
14
15
Yes, it does. It requires the same [ setup] ( setup.mdx ) as a "normal" Svelte
15
16
project.
16
17
18
+ ## ` onMount ` isn't called when rendering components
19
+
20
+ Since the test is running in a Node environment instead of a browser
21
+ environment, it uses the SSR exports from Svelte, which declare
22
+ all lifecycle events as no-ops.
23
+
24
+ One solution is to configure Vite to use browser resolutions in tests.
25
+
26
+ ``` js title="vite.config.js"
27
+
28
+ import { svelte } from ' @sveltejs/vite-plugin-svelte' ;
29
+ import { defineConfig } from ' vite' ;
30
+
31
+ export default defineConfig (({ mode }) => ({
32
+ plugins: [svelte ()],
33
+ resolve: {
34
+ // the default would be [ 'svelte', 'node' ]
35
+ // as set by vite-plugin-svelte and vitest
36
+ conditions: mode === ' test' ? [' browser' ] : [],
37
+ },
38
+ test: {
39
+ environment: ' jsdom' ,
40
+ },
41
+ };
42
+
43
+ ` ` `
44
+
45
+ See
46
+ [svelte-testing-library's issue 222](https://github.com/testing-library/svelte-testing-library/issues/222)
47
+ for more details.
48
+
17
49
## Testing file upload component
18
50
19
51
File upload handler not triggering? Use ` happy- dom` , not ` jsdom` , and make sure
0 commit comments