Skip to main content

FAQ


Does this library also work with SvelteKit?

Yes, it does. It requires the same setup 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.

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 for more details.

Testing file upload component

File upload handler not triggering? Use happy-dom, not jsdom, and make sure to use fireEvent.change(...) and not fireEvent.input(...). It seems that jsdom (which is at v22.1.0 at the time of this writing) doesn't fake all the File object as it should.

See svelte-testing-library's issue 219 for more details.