{"data":{"slug":"github-lobehub-lobehub-agents-skills-testing-ec80a2b80783","title":"Run Vitest Tests for LobeHub Projects","summary":"Execute targeted tests for LobeHub applications, including web, packages, and desktop components, with specific commands for different test categories and environments.","url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-testing-ec80a2b80783","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-lobehub-lobehub-agents-skills-testing-ec80a2b80783","markdown_url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-testing-ec80a2b80783.md","download_url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-testing-ec80a2b80783\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before running tests, ensure the correct Vitest configuration is in place. For database model tests, use `getTestDB()` and guard BM25\/full-text-search blocks with `describe.skipIf(!isServerDB)`. When testing UI components, avoid hand-mocking `@lobehub\/ui\/base-ui` unless simplified DOM is required; use `~base-ui-stubs` for standard aria semantics.","instructions":"---\nname: testing\ndescription: 'Use for Vitest tests, mocks, coverage and failing-test diagnosis.'\nuser-invocable: false\n---\n\n# LobeHub Testing Guide\n\n## Quick Reference\n\n**Commands:**\n\n```bash\n# Run specific test file\nbunx vitest run --silent='passed-only' '[file-path]'\n\n# Database package (client-db, PGlite \u2014 default, no pg_search; BM25 migrations skip themselves)\ncd packages\/database && bunx vitest run --silent='passed-only' '[file]'\n\n# Database package (server-db, Postgres \u2014 BM25\/pgvector parity, what CI measures coverage in)\ncd packages\/database && TEST_SERVER_DB=1 bunx vitest run --silent='passed-only' '[file]'\n```\n\n**Never run** `bun run test` - it runs all 3000+ tests (\\~10 minutes).\n\n> **Database models\/repositories:** every new file under `packages\/database\/src\/models\/**`\n> or `src\/repositories\/**` ships with a sibling `__tests__\/<name>.test.ts` in the same PR.\n> Use the real DB via `getTestDB()` (integration style), guard BM25\/full-text-search blocks\n> with `describe.skipIf(!isServerDB)`, and always test user-isolation. See\n> `references\/db-model-test.md` for setup, schema gotchas, and the client-vs-server-db split.\n\n## Test Categories\n\n| Category | Location                    | Config                          |\n| -------- | --------------------------- | ------------------------------- |\n| Webapp   | `src\/**\/*.test.ts(x)`       | `vitest.config.ts`              |\n| Packages | `packages\/*\/**\/*.test.ts`   | `packages\/*\/vitest.config.ts`   |\n| Desktop  | `apps\/desktop\/**\/*.test.ts` | `apps\/desktop\/vitest.config.ts` |\n\n## Core Principles\n\n1. **Prefer `vi.spyOn` over `vi.mock`** - More targeted, easier to maintain. The root Vitest configs do not restore mocks automatically; restore spies in test cleanup with `vi.restoreAllMocks()`.\n2. **Test behavior, not implementation details**\n   - Keep native `node:test` CI checks under `.github\/scripts\/`, outside the app Vitest discovery paths. When mixing test runners, verify both the intended runner and the owning Vitest project's file discovery before pushing.\n3. **Regression tests for bug fixes** - Include a regression test that fails without the fix and passes with it; write the failing test first when the failure is easy to reproduce. **Skip** pure style\/CSS fixes (selector, hover, mask, spacing, color) when the only practical assertion would be source-string matching on the stylesheet \u2014 that is not a regression test worth shipping.\n4. **No new component tests** - Only update existing React component tests. Complex logic should be extracted into hooks and tested there instead\n\n## UI Library Mocks (@lobehub\/ui\/base-ui)\n\n**Default: do NOT mock `@lobehub\/ui\/base-ui` \u2014 render the real components.**\n`vitest.config.mts` redirects the library's internal MotionProvider to a static\nstub (`tests\/mocks\/lobehubUiMotionProvider.tsx`), so base-ui components render in\ntests without the app-level ConfigProvider. `Please wrap your app with <ConfigProvider> (or <MotionProvider>)` in a test means that redirect is not in\neffect (e.g. a package-local vitest config) \u2014 do not fix it by hand-mocking every\ncomponent.\n\nWhen a test genuinely wants simplified DOM, compose the canonical stubs over the\nreal module instead of writing a closed factory (closed factories break whenever\nthe library migrates a component's import path):\n\n```typescript\nvi.mock('@lobehub\/ui\/base-ui', async (importOriginal) => ({\n  ...(await importOriginal<object>()),\n  ...(await import('~base-ui-stubs')).baseUiStubs,\n}));\n```\n\n`~base-ui-stubs` (`tests\/mocks\/baseUiStubs.tsx`) covers ActionIcon \/ Button \/\nText \/ Tag \/ Avatar \/ Alert \/ toast \/ confirmModal \/ createModal with standard\naria semantics. A per-file factory is still fine when assertions need bespoke\ntestid conventions \u2014 but keep it composed over `importOriginal` so unknown\nexports never go missing.\n\n## Detailed Guides\n\nSee `references\/` for specific testing scenarios:\n\n- **Database Model testing**: `references\/db-model-test.md`\n- **Electron IPC testing**: `references\/electron-ipc-test.md`\n- **Zustand Store Action testing**: `references\/zustand-store-action-test.md`\n- **Agent Runtime E2E testing**: `references\/agent-runtime-e2e.md`\n- **Desktop Controller testing**: `references\/desktop-controller-test.md`\n\n## Fixing Failing Tests \u2014 Optimize or Delete?\n\nWhen tests fail due to implementation changes (not bugs), evaluate before blindly fixing:\n\n### Keep & Fix (update test data\/assertions)\n\n- **Behavior tests**: Tests that verify _what_ the code does (output, side effects, user-visible behavior). Just update mock data formats or expected values.\n  - Example: Tool data structure changed from `{ name }` to `{ function: { name } }` \u2192 update mock data\n  - Example: Output format changed from `Current date: YYYY-MM-DD` to `Current date: YYYY-MM-DD (TZ)` \u2192 update expected string\n\n### Delete (over-specified, low value)\n\n- **Param-forwarding tests**: Tests that assert exact internal function call arguments (e.g., `expect(internalFn).toHaveBeenCalledWith(expect.objectContaining({ exact params }))`) \u2014 these break on every refactor and duplicate what behavior tests already cover.\n- **Implementation-coupled tests**: Tests that verify _how_ the code works internally rather than _what_ it produces. If a higher-level test already covers the same behavior, the low-level test adds maintenance cost without coverage gain.\n\n### Decision Checklist\n\n1. Does the test verify **externally observable behavior** (API response, DB write, rendered output)? \u2192 **Keep**\n2. Does the test only verify **internal wiring** (which function receives which params)? \u2192 Check if a behavior test already covers it. If yes \u2192 **Delete**\n3. Is the same behavior already tested at a **higher integration level**? \u2192 Delete the lower-level duplicate\n4. Would the test break again on the **next routine refactor**? \u2192 Consider raising to integration level or deleting\n\n### When Writing New Tests\n\n- Prefer **integration-level assertions** (verify final output) over **white-box assertions** (verify internal calls)\n- Use `expect.objectContaining` only for stable, public-facing contracts \u2014 not for internal param shapes that change with refactors\n- Mock at boundaries (DB, network, external services), not between internal modules\n","use_cases":["Run specific test files for web application components","Execute database model tests with integration-style real DB","Test desktop application functionality with proper configuration"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/lobehub\/lobehub\/blob\/54382b5c8e81544da454fa6d7b50f461a562c1ee\/.agents\/skills\/testing\/SKILL.md","source_commit":"54382b5c8e81544da454fa6d7b50f461a562c1ee","license":null,"version":null,"listing_approved_at":"2026-11-22","created_at":"2026-11-21T11:37:44.000000Z","updated_at":"2026-11-22T11:17:19.000000Z","seo_title":"Run Vitest Tests for LobeHub Projects","seo_description":"Execute targeted tests for LobeHub applications, including web, packages, and desktop components with precise command execution and environment-specific configurations.","seo_keywords":["run vitest tests lobehub","execute webapp tests lobehub","test database models lobehub","desktop app testing lobehub","vitest package tests lobehub"]}}