Testing
Testing stores requires understanding lifecycle behavior, waiting for async operations, and mocking dependencies through Dependency Injection.
Debounced Unmounting
Section titled “Debounced Unmounting”Unmounting in onMount is debounced to handle rapid mount/unmount cycles during component re-renders. The delay between unmount and mount events is defined in STORE_UNMOUNT_DELAY (1000ms). Mount events fire immediately without delay.
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'import { STORE_UNMOUNT_DELAY, mountable, signal, onMount, start } from '@nano_kit/store'
beforeEach(() => { vi.useFakeTimers()})
afterEach(() => { vi.useRealTimers()})
it('should cleanup after unmount delay', () => { const $data = mountable(signal(0)) const cleanup = vi.fn()
onMount($data, () => cleanup)
const stop = start($data)
expect(cleanup).toHaveBeenCalledTimes(0)
stop()
/* Cleanup not called immediately */ expect(cleanup).toHaveBeenCalledTimes(0)
/* Advance timers to trigger unmount */ vi.advanceTimersByTime(STORE_UNMOUNT_DELAY)
expect(cleanup).toHaveBeenCalledTimes(1)})Triggering Mount Events
Section titled “Triggering Mount Events”Use start to create an effect that keeps a signal alive, triggering its mount event. This is useful for testing mountable stores.
import { expect, vi } from 'vitest'import { start, mountable, signal, onMount } from '@nano_kit/store'
const $data = mountable(signal(0))const mounted = vi.fn()
onMount($data, mounted)
/* Start keeps $data mounted */const stop = start($data)
expect(mounted).toHaveBeenCalledTimes(1)
/* Cleanup triggers unmount after debounce delay */stop()For one-time execution without keeping the signal alive, use exec:
import { expect, vi } from 'vitest'import { exec, mountable, signal, onMount } from '@nano_kit/store'
const $data = mountable(signal(0))const mounted = vi.fn()
onMount($data, mounted)
/* Triggers mount, then immediately unmounts */exec($data)
expect(mounted).toHaveBeenCalledTimes(1)Waiting for Async Operations
Section titled “Waiting for Async Operations”Tasks are useful not only for SSR but also in tests to wait for async operations inside effects. Pass the signal under test to waitTasks to ensure its async work completes before assertions.
import { expect, vi } from 'vitest'import { mountable, signal, computed, onMount, start, task, waitTasks } from '@nano_kit/store'
const fetchName = vi.fn().mockResolvedValue('John')const $name = mountable(signal<string | null>(null))const $greeting = computed(() => `Hello, ${$name() ?? 'guest'}`)
/* The store attaches its request to the signal it fills */onMount($name, () => { void task($name, async () => { $name(await fetchName()) })})
/* Mount $greeting and the $name it reads */const stop = start($greeting)
/* Wait for the tasks of $greeting and of every signal it depends on */await waitTasks($greeting)
expect($greeting()).toBe('Hello, John')
stop()The waitTasks function waits until neither the signal nor anything it depends on has tasks left, including tasks that spawn new tasks. It follows the links the graph already has, so start the signal before waiting: a computed links its dependencies on its first read. Queries, mutations and resolved attach their tasks by themselves. waitTasks resolves even when a task fails, so assert the error state after it.
Mocking Dependencies
Section titled “Mocking Dependencies”Use the DI mechanism to mock dependencies in tests. Create a custom InjectionContext with provide to inject test doubles.
import { InjectionContext, provide, inject } from '@nano_kit/store'import { virtualNavigation } from '@nano_kit/router'import { Location$, Navigation$ } from './router'import { Episodes$ } from './episodes'
/* Create virtual navigation for testing */const [$location, navigation] = virtualNavigation('/episodes/1', { episodes: '/episodes/:id'})
/* Inject mocked dependencies */const context = new InjectionContext([ provide(Location$, $location), provide(Navigation$, navigation)])
/* Inject store with mocked context */const { $episodes } = inject(Episodes$, context)