testing by lobehub/lobehub
npx skills add https://github.com/lobehub/lobehub --skill testing命令:
# 运行特定测试文件
bunx vitest run --silent='passed-only' '[file-path]'
# 数据库包(客户端)
cd packages/database && bunx vitest run --silent='passed-only' '[file]'
# 数据库包(服务器)
cd packages/database && TEST_SERVER_DB=1 bunx vitest run --silent='passed-only' '[file]'
切勿运行 bun run test - 它会运行所有 3000 多个测试(约 10 分钟)。
| 类别 | 位置 | 配置 |
|---|---|---|
| Web 应用 | src/**/*.test.ts(x) | vitest.config.ts |
| 包 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
packages/*/**/*.test.ts |
packages/*/vitest.config.ts |
| 桌面端 | apps/desktop/**/*.test.ts | apps/desktop/vitest.config.ts |
vi.spyOn 而非 vi.mock - 更有针对性,更易于维护bun run type-checkimport { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('ModuleName', () => {
describe('functionName', () => {
it('should handle normal case', () => {
// 准备 → 执行 → 断言
});
});
});
// ✅ 监视直接依赖项
vi.spyOn(messageService, 'createMessage').mockResolvedValue('id');
// ✅ 对浏览器 API 使用 vi.stubGlobal
vi.stubGlobal('Image', mockImage);
vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:mock');
// ❌ 避免全局模拟整个模块
vi.mock('@/services/chat'); // 范围过大
查看 references/ 目录下的特定测试场景:
references/db-model-test.mdreferences/electron-ipc-test.mdreferences/zustand-store-action-test.mdreferences/agent-runtime-e2e.mdreferences/desktop-controller-test.md当测试因实现变更(而非错误)而失败时,在盲目修复前请先评估:
{ name } 更改为 { function: { name } } → 更新模拟数据当前日期: YYYY-MM-DD 更改为 当前日期: YYYY-MM-DD (时区) → 更新期望字符串expect(internalFn).toHaveBeenCalledWith(expect.objectContaining({ exact params })))——这些测试在每次重构时都会中断,并且重复了行为测试已经覆盖的内容。expect.objectContaining —— 不用于随重构而变化的内部参数结构vi.resetModules()vi.clearAllMocks()act() 中每周安装量
494
仓库
GitHub 星标数
74.1K
首次出现
2026 年 1 月 27 日
安全审计
安装于
opencode428
gemini-cli424
codex424
github-copilot407
cursor363
claude-code352
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 hooksWeekly Installs
494
Repository
GitHub Stars
74.1K
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode428
gemini-cli424
codex424
github-copilot407
cursor363
claude-code352
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装