Skip to content

Commit 2cd12ab

Browse files
authored
docs(svelte-testing-library): add FAQ entry for transition events (#1363)
1 parent 6ae3bb1 commit 2cd12ab

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

docs/svelte-testing-library/faq.mdx

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,97 @@ sidebar_label: FAQ
55
---
66

77
- [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)
1011

1112
---
1213

1314
## Does this library also work with SvelteKit?
1415

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.
1717

18-
## `onMount` isn't called when rendering components
18+
## Why isn't `onMount` called when rendering components?
1919

2020
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.
2323

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.
2525

2626
```js title="vite.config.js"
27+
import {svelte} from '@sveltejs/vite-plugin-svelte'
28+
import {defineConfig} from 'vite'
2729

28-
import { svelte } from '@sveltejs/vite-plugin-svelte';
29-
import { defineConfig } from 'vite';
30-
31-
export default defineConfig(({ mode }) => ({
30+
export default defineConfig(({mode}) => ({
3231
plugins: [svelte()],
3332
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' ]
3636
conditions: mode === 'test' ? ['browser'] : [],
3737
},
3838
test: {
3939
environment: 'jsdom',
4040
},
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()
4258

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+
})
4369
```
4470

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:
4879

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+
```
5095

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.
5597

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

Comments
 (0)