Skip to content

Commit 09696f9

Browse files
authored
svelte: add FAQ entry for onMount (#1361)
1 parent ac096ab commit 09696f9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/svelte-testing-library/faq.mdx

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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)
89
- [Testing file upload component](#testing-file-upload-component)
910

1011
---
@@ -14,6 +15,37 @@ sidebar_label: FAQ
1415
Yes, it does. It requires the same [setup](setup.mdx) as a "normal" Svelte
1516
project.
1617

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+
1749
## Testing file upload component
1850
1951
File upload handler not triggering? Use `happy-dom`, not `jsdom`, and make sure

0 commit comments

Comments
 (0)