@@ -5,54 +5,97 @@ 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 )
9
- - [ Testing file upload component] ( #testing-file-upload-component )
8
+ - [ Why isn't ` onMount ` called when rendering components?] ( #why-isnt-onmount-called-when-rendering-components )
9
+ - [ How do I test file upload?] ( #how-do-i-test-file-upload )
10
+ - [ Why aren't transition events running?] ( #why-arent-transition-events-running )
10
11
11
12
---
12
13
13
14
## Does this library also work with SvelteKit?
14
15
15
- Yes, it does. It requires the same [ setup] ( setup.mdx ) as a "normal" Svelte
16
- project.
16
+ Yes, it does. It requires the same [ setup] [ ] as a "normal" Svelte project.
17
17
18
- ## ` onMount ` isn't called when rendering components
18
+ ## Why isn't ` onMount ` called when rendering components?
19
19
20
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.
21
+ environment, it uses the SSR exports from Svelte, which declare all lifecycle
22
+ events as no-ops.
23
23
24
- One solution is to configure Vite to use browser resolutions in tests.
24
+ One solution is to configure Vite to use browser resolutions in tests.
25
25
26
26
``` js title="vite.config.js"
27
+ import {svelte } from ' @sveltejs/vite-plugin-svelte'
28
+ import {defineConfig } from ' vite'
27
29
28
- import { svelte } from ' @sveltejs/vite-plugin-svelte' ;
29
- import { defineConfig } from ' vite' ;
30
-
31
- export default defineConfig (({ mode }) => ({
30
+ export default defineConfig (({mode}) => ({
32
31
plugins: [svelte ()],
33
32
resolve: {
34
- // the default would be [ 'svelte', 'node' ]
35
- // as set by vite-plugin-svelte and vitest
33
+ // The default would be [ 'svelte', 'node' ]
34
+ // as set by vite-plugin-svelte and vitest.
35
+ // This sets [ 'browser', 'svelte', 'node' ]
36
36
conditions: mode === ' test' ? [' browser' ] : [],
37
37
},
38
38
test: {
39
39
environment: ' jsdom' ,
40
40
},
41
- };
41
+ }))
42
+ ```
43
+
44
+ See svelte-testing-library's [ issue 222] [ ] for more details.
45
+
46
+ [ setup ] : ./setup.mdx
47
+ [ issue 222] :
48
+ https://github.com/testing-library/svelte-testing-library/issues/222
49
+
50
+ ## How do I test file upload?
51
+
52
+ Use the [ upload] [ ] utility from ` @testing-library/user-event ` rather than
53
+ ` fireEvent ` . It works well in both [ jsdom] [ ] and [ happy-dom] [ ] .
54
+
55
+ ``` js
56
+ test (' upload file' , async () => {
57
+ const user = userEvent .setup ()
42
58
59
+ render (Uploader)
60
+ const file = new File ([' hello' ], ' hello.png' , {type: ' image/png' })
61
+ const input = screen .getByLabelText (/ upload file/ i )
62
+
63
+ await user .upload (input, file)
64
+
65
+ expect (input .files [0 ]).toBe (file)
66
+ expect (input .files .item (0 )).toBe (file)
67
+ expect (input .files ).toHaveLength (1 )
68
+ })
43
69
```
44
70
45
- See
46
- [svelte-testing-library's issue 222](https://github.com/testing-library/svelte-testing-library/issues/222)
47
- for more details.
71
+ [ upload ] : ../user-event/api-utility.mdx#upload
72
+ [ jsdom ] : https://github.com/jsdom/jsdom
73
+ [ happy-dom ] : https://github.com/capricorn86/happy-dom
74
+
75
+ ## Why aren't [ transition events] [ ] running?
76
+
77
+ The [ jsdom] [ ] implementation of ` requestAnimationFrame ` can be unreliable in
78
+ Vitest. To work around it, you can:
48
79
49
- ## Testing file upload component
80
+ - Switch to [ happy-dom] [ ] , if you are able, which does not exhibit the same
81
+ issues
82
+ - Replace the implementation of ` requestAnimationFrame `
83
+
84
+ ``` js
85
+ beforeEach (() => {
86
+ const raf = fn => setTimeout (() => fn (new Date ()), 16 )
87
+ vi .stubGlobal (' requestAnimationFrame' , raf)
88
+ })
89
+
90
+ // Alternatively, set `unstubGlobals: true` in vitest.config.js
91
+ afterEach (() => {
92
+ vi .unstubAllGlobals ()
93
+ })
94
+ ```
50
95
51
- File upload handler not triggering? Use ` happy- dom` , not ` jsdom` , and make sure
52
- to use ` fireEvent .change (... )` and not ` fireEvent .input (... )` . It seems that
53
- jsdom (which is at v22.1.0 at the time of this writing) doesn't fake all the
54
- File object as it should.
96
+ See svelte-testing-library's [ issue 206] [ ] for more details.
55
97
56
- See
57
- [svelte-testing-library's issue 219](https://github.com/testing-library/svelte-testing-library/issues/219)
58
- for more details.
98
+ [ transition events] :
99
+ https://svelte.dev/docs/element-directives#transition-events
100
+ [ issue 206] :
101
+ https://github.com/testing-library/svelte-testing-library/issues/206
0 commit comments