- For JavaScript: Only write Vitest tests when resolving a specific user issue or upon explicit request.
- Import core functions as `import { describe, it, expect } from 'vitest';`.
- Use jsdom environment for DOM-related tests and node for others.
- Write focused, isolated test cases.
- Configure Vitest coverage with exclusion patterns and multiple report formats.
- For JavaScript: Import from `vitest`: `import { describe, it, expect, vi, beforeEach } from 'vitest'` — do not mix with Jest's globals even if `globals: true` is configured.
- Mock modules with `vi.mock('../db', () => ({ query: vi.fn() }))` at the top of the test file — Vitest hoists `vi.mock` calls before imports automatically.
- Use `vi.spyOn(obj, 'method').mockResolvedValue(result)` to patch individual methods without replacing the whole module.
- Assert async operations: `await expect(asyncFn()).resolves.toEqual({ id: 1 })` and `await expect(failingFn()).rejects.toThrow('message')`.
- Reset mocks between tests: `beforeEach(() => vi.clearAllMocks())` — use `vi.restoreAllMocks()` in `afterEach` when using spies.
- Configure per-file environment in the file header: `// @vitest-environment jsdom` — or set globally in `vitest.config.ts` `test.environment: 'jsdom'`.