npx skills add https://github.com/lobehub/lobe-chat --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 } } → 更新模拟数据Current date: YYYY-MM-DD 更改为 Current date: YYYY-MM-DD (TZ) → 更新期望字符串expect(internalFn).toHaveBeenCalledWith(expect.objectContaining({ exact params })))— 这些测试在每次重构时都会中断,并且重复了行为测试已覆盖的内容。expect.objectContaining — 不用于随重构而变化的内部参数结构vi.resetModules()vi.clearAllMocks()act() 中每周安装量
153
代码仓库
GitHub 星标
74.2K
首次出现
2026 年 1 月 24 日
安全审计
安装于
claude-code148
opencode139
cursor130
gemini-cli129
codex126
antigravity125
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
153
Repository
GitHub Stars
74.2K
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code148
opencode139
cursor130
gemini-cli129
codex126
antigravity125
Vue 3 调试指南:解决响应式、计算属性与监听器常见错误
11,600 周安装