Svelte · Middle · Question 30 of 75
How do you test an effect created outside a component?
const cleanup = $effect.root(() => {
$effect(() => log.push(count));
});
// ...
cleanup();- ✓Correct answer: Wrap it in
$effect.root, which creates a scope you clean up manually - 2Effects can only run inside components
- 3Call
$effectdirectly in the test
Explanation
$effect must run inside a parent effect or component. $effect.root provides that context in tests and long-lived logic.