javascript / expert
Snippet
Isolated Logic Validation for Runes
Expert Svelte testing involves validating reactive logic in isolation. Because runes like $derived and $effect update asynchronously for performance, flushSync must be used in unit tests to force microtask execution before asserting the results.
snippet.js
1
2
3
4
5
6
7
8
9
10
11
// logic.test.tsimport { flushSync } from 'svelte';test('reactive increment', () => {let count = $state(0);let double = $derived(count * 2);count = 5;flushSync();expect(double).toBe(10);});
svelte
Breakdown
1
import { flushSync } from 'svelte'
Imports the utility required to synchronize reactive updates in a non-component environment.
2
flushSync()
Immediately processes all pending state changes and effects.