|
| 1 | +--- |
| 2 | +globs: |
| 3 | + - '**/*.test.ts' |
| 4 | + - '**/*.spec.ts' |
| 5 | +--- |
| 6 | + |
| 7 | +# Vitest Patterns |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +Use `createTestingPinia` from `@pinia/testing`, not `createPinia`: |
| 12 | + |
| 13 | +```typescript |
| 14 | +import { createTestingPinia } from '@pinia/testing' |
| 15 | +import { setActivePinia } from 'pinia' |
| 16 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 17 | + |
| 18 | +describe('MyStore', () => { |
| 19 | + beforeEach(() => { |
| 20 | + setActivePinia(createTestingPinia({ stubActions: false })) |
| 21 | + vi.useFakeTimers() |
| 22 | + vi.resetAllMocks() |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + vi.useRealTimers() |
| 27 | + }) |
| 28 | +}) |
| 29 | +``` |
| 30 | + |
| 31 | +**Why `stubActions: false`?** By default, testing pinia stubs all actions. Set to `false` when testing actual store behavior. |
| 32 | + |
| 33 | +## Mock Patterns |
| 34 | + |
| 35 | +### Reset all mocks at once |
| 36 | + |
| 37 | +```typescript |
| 38 | +beforeEach(() => { |
| 39 | + vi.resetAllMocks() // Not individual mock.mockReset() calls |
| 40 | +}) |
| 41 | +``` |
| 42 | + |
| 43 | +### Module mocks with vi.mock() |
| 44 | + |
| 45 | +```typescript |
| 46 | +vi.mock('@/scripts/api', () => ({ |
| 47 | + api: { |
| 48 | + addEventListener: vi.fn(), |
| 49 | + fetchData: vi.fn() |
| 50 | + } |
| 51 | +})) |
| 52 | + |
| 53 | +vi.mock('@/services/myService', () => ({ |
| 54 | + myService: { |
| 55 | + doThing: vi.fn() |
| 56 | + } |
| 57 | +})) |
| 58 | +``` |
| 59 | + |
| 60 | +### Configure mocks in tests |
| 61 | + |
| 62 | +```typescript |
| 63 | +import { api } from '@/scripts/api' |
| 64 | +import { myService } from '@/services/myService' |
| 65 | + |
| 66 | +it('handles success', () => { |
| 67 | + vi.mocked(myService.doThing).mockResolvedValue({ data: 'test' }) |
| 68 | + // ... test code |
| 69 | +}) |
| 70 | +``` |
| 71 | + |
| 72 | +## Testing Event Listeners |
| 73 | + |
| 74 | +When a store registers event listeners at module load time: |
| 75 | + |
| 76 | +```typescript |
| 77 | +function getEventHandler() { |
| 78 | + const call = vi.mocked(api.addEventListener).mock.calls.find( |
| 79 | + ([event]) => event === 'my_event' |
| 80 | + ) |
| 81 | + return call?.[1] as (e: CustomEvent<MyEventType>) => void |
| 82 | +} |
| 83 | + |
| 84 | +function dispatch(data: MyEventType) { |
| 85 | + const handler = getEventHandler() |
| 86 | + handler(new CustomEvent('my_event', { detail: data })) |
| 87 | +} |
| 88 | + |
| 89 | +it('handles events', () => { |
| 90 | + const store = useMyStore() |
| 91 | + dispatch({ field: 'value' }) |
| 92 | + expect(store.items).toHaveLength(1) |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +## Testing with Fake Timers |
| 97 | + |
| 98 | +For stores with intervals, timeouts, or polling: |
| 99 | + |
| 100 | +```typescript |
| 101 | +beforeEach(() => { |
| 102 | + vi.useFakeTimers() |
| 103 | +}) |
| 104 | + |
| 105 | +afterEach(() => { |
| 106 | + vi.useRealTimers() |
| 107 | +}) |
| 108 | + |
| 109 | +it('polls after delay', async () => { |
| 110 | + const store = useMyStore() |
| 111 | + store.startPolling() |
| 112 | + |
| 113 | + await vi.advanceTimersByTimeAsync(30000) |
| 114 | + |
| 115 | + expect(mockService.fetch).toHaveBeenCalled() |
| 116 | +}) |
| 117 | +``` |
| 118 | + |
| 119 | +## Assertion Style |
| 120 | + |
| 121 | +Prefer `.toHaveLength()` over `.length.toBe()`: |
| 122 | + |
| 123 | +```typescript |
| 124 | +// Good |
| 125 | +expect(store.items).toHaveLength(1) |
| 126 | + |
| 127 | +// Avoid |
| 128 | +expect(store.items.length).toBe(1) |
| 129 | +``` |
| 130 | + |
| 131 | +Use `.toMatchObject()` for partial matching: |
| 132 | + |
| 133 | +```typescript |
| 134 | +expect(store.completedItems[0]).toMatchObject({ |
| 135 | + id: 'task-123', |
| 136 | + status: 'done' |
| 137 | +}) |
| 138 | +``` |
0 commit comments