@@ -5,54 +5,95 @@ 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' ]
33
+ // the default would be [ 'svelte', 'node' ]
35
34
// as set by vite-plugin-svelte and vitest
36
35
conditions: mode === ' test' ? [' browser' ] : [],
37
36
},
38
37
test: {
39
38
environment: ' jsdom' ,
40
39
},
41
- };
40
+ }))
41
+ ```
42
+
43
+ See svelte-testing-library's [ issue 222] [ ] for more details.
44
+
45
+ [ setup ] : ./setup.mdx
46
+ [ issue 222] :
47
+ https://github.com/testing-library/svelte-testing-library/issues/222
48
+
49
+ ## How do I test file upload?
50
+
51
+ Use the [ upload] [ ] utility from ` @testing-library/user-event ` rather than
52
+ ` fireEvent ` . It works well in both [ jsdom] [ ] and [ happy-dom] [ ] .
53
+
54
+ ``` js
55
+ test (' upload file' , async () => {
56
+ const user = userEvent .setup ()
42
57
58
+ render (Uploader)
59
+ const file = new File ([' hello' ], ' hello.png' , {type: ' image/png' })
60
+ const input = screen .getByLabelText (/ upload file/ i )
61
+
62
+ await user .upload (input, file)
63
+
64
+ expect (input .files [0 ]).toBe (file)
65
+ expect (input .files .item (0 )).toBe (file)
66
+ expect (input .files ).toHaveLength (1 )
67
+ })
43
68
```
44
69
45
- See
46
- [svelte-testing-library's issue 222](https://github.com/testing-library/svelte-testing-library/issues/222)
47
- for more details.
70
+ [ upload ] : https://testing-library.com/docs/user-event/utility#upload
71
+ [ jsdom ] : https://github.com/jsdom/jsdom
72
+ [ happy-dom ] : https://github.com/capricorn86/happy-dom
73
+
74
+ ## Why aren't [ transition events] [ ] running?
75
+
76
+ The [ jsdom] [ ] ` requestAnimationFrame ` implementation can be unreliable in
77
+ Vitest. To work around it, you can try a few things:
48
78
49
- ## Testing file upload component
79
+ - Switch [ happy-dom] [ ] , if you are able, which does not exhibit the same issues
80
+ - Replace the implementation of ` requestAnimationFrame `
81
+
82
+ ``` js
83
+ beforeEach (() => {
84
+ const raf = fn => setTimeout (() => fn (new Date ()), 16 )
85
+ vi .stubGlobal (' requestAnimationFrame' , raf)
86
+ })
87
+
88
+ // Alternatively, set `unstubGlobals: true` in vitest.config.js
89
+ afterEach (() => {
90
+ vi .unstubAllGlobals ()
91
+ })
92
+ ```
50
93
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.
94
+ See svelte-testing-library's [ issue 206] [ ] for more details.
55
95
56
- See
57
- [svelte-testing-library's issue 219](https://github.com/testing-library/svelte-testing-library/issues/219)
58
- for more details.
96
+ [ transition events] :
97
+ https://svelte.dev/docs/element-directives#transition-events
98
+ [ issue 206] :
99
+ https://github.com/testing-library/svelte-testing-library/issues/206
0 commit comments