Skip to content

svelte: add FAQ entry for onMount #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/svelte-testing-library/faq.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sidebar_label: FAQ
---

- [Does this library also work with SvelteKit?](#does-this-library-also-work-with-sveltekit)
- [`onMount` isn't called when rendering components](#onmount-isnt-called-when-rendering-compoments)
- [Testing file upload component](#testing-file-upload-component)

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

## `onMount` isn't called when rendering components

Since the test is running in a Node environment instead of a browser
environment, it uses the SSR exports from Svelte, which declare
all lifecycle events as no-ops.

One solution is to configure Vite to use browser resolutions in tests.

```js title="vite.config.js"

import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vite';

export default defineConfig(({ mode }) => ({
plugins: [svelte()],
resolve: {
// the default would be [ 'svelte', 'node' ]
// as set by vite-plugin-svelte and vitest
conditions: mode === 'test' ? ['browser'] : [],
},
test: {
environment: 'jsdom',
},
};

```

See
[svelte-testing-library's issue 222](https://github.com/testing-library/svelte-testing-library/issues/222)
for more details.

## Testing file upload component

File upload handler not triggering? Use `happy-dom`, not `jsdom`, and make sure
Expand Down