Testing guide using Vitest. Use when writing tests (.test.ts, .test.tsx), fixing failing tests, improving test coverage, or debugging test issues. Triggers on test creation, test debugging, mock setup, or test-related questions.
Commands:
# Run specific test file
bunx vitest run --silent='passed-only' '[file-path]'
# Database package (client)
cd packages/database && bunx vitest run --silent='passed-only' '[file]'
# Database package (server)
cd packages/database && TEST_SERVER_DB=1 bunx vitest run --silent='passed-only' '[file]'
Never run bun run test - it runs all 3000+ tests (~10 minutes).
| Category | Location | Config |
|---|---|---|
| Webapp | src/**/*.test.ts(x) | vitest.config.ts |
| Packages | packages/*/**/*.test.ts |
packages/*/vitest.config.ts |
| Desktop | apps/desktop/**/*.test.ts | apps/desktop/vitest.config.ts |
vi.spyOn over vi.mock - More targeted, easier to maintainbun run type-check after writing testsimport { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('ModuleName', () => {
describe('functionName', () => {
it('should handle normal case', () => {
// Arrange → Act → Assert
});
});
});
// ✅ Spy on direct dependencies
vi.spyOn(messageService, 'createMessage').mockResolvedValue('id');
// ✅ Use vi.stubGlobal for browser APIs
vi.stubGlobal('Image', mockImage);
vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock');
// ❌ Avoid mocking entire modules globally
vi.mock('@/services/chat'); // Too broad
See references/ for specific testing scenarios:
references/db-model-test.mdreferences/electron-ipc-test.mdreferences/zustand-store-action-test.mdreferences/agent-runtime-e2e.mdreferences/desktop-controller-test.mdWhen tests fail due to implementation changes (not bugs), evaluate before blindly fixing:
{ name } to { function: { name } } → update mock dataCurrent date: YYYY-MM-DD to Current date: YYYY-MM-DD (TZ) → update expected stringexpect(internalFn).toHaveBeenCalledWith(expect.objectContaining({ exact params }))) — these break on every refactor and duplicate what behavior tests already cover.expect.objectContaining only for stable, public-facing contracts — not for internal param shapes that change with refactorsvi.resetModules() when tests fail mysteriouslyvi.clearAllMocks() in beforeEachact() for React hooks