How to use createRawSnippet to test components? #13518
Unanswered
unikitty37
asked this question in
Q&A
Replies: 1 comment 3 replies
-
<script>
import { createRawSnippet, mount, unmount } from 'svelte';
import Component from "./Component.svelte";
import Container from "./Container.svelte"
const props = $state({ name: "Test" })
const snip = createRawSnippet((name) => {
return {
render: () => `<div></div>`,
setup: (target) => {
const comp = mount(Component, { target, props });
return () => unmount(comp);
}
};
});
</script>
<input bind:value={props.name}><br>
<Container children={snip} /> |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In #2588, we're told that we can use Svelte 5's
createRawSnippet
to programmatically create child snippets (or "slots", in the old language). The docs show it being used as{@render greet(name)}
but this doesn't give much of a clue on how to use it in a Vitest/Testing Library test.Additionally, despite the switch at lower left being set to TypeScript, the example code is pure JavaScript and I don't know what types to use.
The result is that this code produces the error "Argument of type '[string]' is not assignable to parameter of type 'never'" and the test doesn't work as expected.
I also get "'Badge' is defined but never used", because TypeScript isn't able to parse the template string. I'd rather have a way of rendering the component that doesn't use template strings, TBH — if I'd wanted to pepper my code with those, I'd use React 😁
What should I be putting here? (It would be really helpful if the docs actually covered the use of this for writing tests, as that's likely to be one of the main uses…)
Beta Was this translation helpful? Give feedback.
All reactions