工作室测试
- 作者仓库星标 103,397
- 作者仓库 supabase
- 领域
- 写作
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @supabase · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- 未声明(默认跨平台)
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 命令执行
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# Studio Testing Strategy
How to write and structure tests for `apps/studio/`. The core principle: push
logic out of React components into pure utility functions, then test those
functions exhaustively. Only use component tests for complex UI interactions.
Use E2E tests for features shared between self-hosted and platform.
## When to Apply
Reference these guidelines when:
- Writing new tests for Studio code
- Deciding which type of test to write (unit, component, E2E)
- Extracting logic from a component to make it testable
- Reviewing whether test coverage is sufficient
- Adding a new feature that needs tests
## Rule Categories by Priority
| Priority | Category | Impact | Prefix |
| -------- | ---------------- | -------- | ---------- |
| 1 | Logic Extraction | CRITICAL | `testing-` |
| 2 | Test Coverage | CRITICAL | `testing-` |
| 3 | Component Tests | HIGH | `testing-` |
| 4 | E2E Tests | HIGH | `testing-` |
## Quick Reference
### 1. Logic Extraction (CRITICAL)
- `testing-extract-logic` - Remove logic from components into `.utils.ts` files
as pure functions: args in, return out
### 2. Test Coverage (CRITICAL)
- `testing-exhaustive-permutations` - Test every permutation of utility functions:
happy path, malformed input, empty values, edge cases
### 3. Component Tests (HIGH)
- `testing-component-tests-ui-only` - Only write component tests for complex UI
interaction logic, not business logic
### 4. E2E Tests (HIGH)
- `testing-e2e-shared-features` - Write E2E tests for features used in both
self-hosted and platform; cover clicks AND keyboard shortcuts
## Decision Tree: Which Test Type?
```
Is the logic a pure transformation (parse, format, validate, compute)?
YES -> Extract to .utils.ts, write unit test with vitest
NO -> Does the feature involve complex UI interactions?
YES -> Is it used in both self-hosted and platform?
YES -> Write E2E test in e2e/studio/features/
NO -> Write component test with customRender
NO -> Can you extract the logic to make it pure?
YES -> Do that, then unit test it
NO -> Write a component test
```
## 1. Extract Logic Into Utility Files (CRITICAL)
Remove as much logic from components as possible. Put it in co-located
`.utils.ts` files as pure functions: arguments in, return value out.
**File naming:**
- Utility: `ComponentName.utils.ts` next to the component
- Test: `tests/components/.../ComponentName.utils.test.ts` mirroring the source path
```tsx
// ❌ Logic buried in component — hard to test without rendering
function TaxIdForm({ taxIdValue, taxIdName }: Props) {
const handleSubmit = () => {
const taxId = TAX_IDS.find((t) => t.name === taxIdName)
let sanitized = taxIdValue
if (taxId?.vatPrefix && !taxIdValue.startsWith(taxId.vatPrefix)) {
sanitized = taxId.vatPrefix + taxIdValue
}
submitToApi(sanitized)
}
return <form onSubmit={handleSubmit}>...</form>
}
// ✅ Logic extracted to .utils.ts — trivially testable
// TaxID.utils.ts
export function sanitizeTaxIdValue({ value, name }: { value: string; name: string }): string {
const taxId = TAX_IDS.find((t) => t.name === name)
if (taxId?.vatPrefix && !value.startsWith(taxId.vatPrefix)) {
return taxId.vatPrefix + value
}
return value
}
// TaxIdForm.tsx — thin shell
const handleSubmit = () => {
const sanitized = sanitizeTaxIdValue({ value: taxIdValue, name: taxIdName })
submitToApi(sanitized)
}
```
## 2. Test Every Permutation (CRITICAL)
Once logic is extracted, test exhaustively. Every code path needs a test:
- Valid inputs (happy path for each branch)
- Invalid / malformed inputs
- Empty values, null values, missing fields
- Edge cases (timestamps with colons, special characters, boundary values)
```ts
// ❌ Only happy path
test('parses a filter', () => {
expect(formatFilterURLParams('id:gte:20')).toStrictEqual({ column: 'id', operator: 'gte', value: '20' })
})
// ✅ Every permutation
test('parses valid filter', () => { ... })
test('handles timestamp with colons in value', () => { ... })
test('rejects malformed filter with missing parts', () => { ... })
test('rejects unrecognized operator', () => { ... })
test('allows empty filter value', () => { ... })
```
## 3. Component Tests for Complex UI Only (HIGH)
Only write component tests when there is complex UI interaction logic that
cannot be captured by testing utility functions alone.
**Valid reasons:** conditional rendering from user interaction sequences,
popover open/close with keyboard/mouse, multi-step form transitions.
**Not valid:** testing a calculation or transformation that happens to live
in a component — extract to `.utils.ts` and unit test instead.
```tsx
// Studio component test conventions
import { fireEvent } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { customRender } from 'tests/lib/custom-render' // always use customRender, not raw render
import { addAPIMock } from 'tests/lib/msw' // API mocking in beforeEach
```
## 4. E2E Tests for Shared Features (HIGH)
If a feature exists in both self-hosted and platform, create an E2E test.
Cover mouse clicks AND keyboard shortcuts (Tab, Enter, Escape, Arrow keys).
Extract reusable interactions into `e2e/studio/utils/*-helpers.ts`. Use
try/finally for resource cleanup. For E2E execution details, see the
`studio-e2e-tests` skill.
## Codebase References
| What | Where |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Util test examples | `apps/studio/tests/components/Grid/Grid.utils.test.ts`, `apps/studio/tests/components/Billing/TaxID.utils.test.ts`, `apps/studio/tests/components/Editor/SpreadsheetImport.utils.test.ts` |
| Component test examples | `apps/studio/tests/features/logs/LogsFilterPopover.test.tsx`, `apps/studio/tests/components/CopyButton.test.tsx` |
| E2E test example | `e2e/studio/features/filter-bar.spec.ts` |
| E2E helpers pattern | `e2e/studio/utils/filter-bar-helpers.ts` |
| Custom render | `apps/studio/tests/lib/custom-render.tsx` |
| MSW mock setup | `apps/studio/tests/lib/msw.ts` (`addAPIMock`) |
| Test README | `apps/studio/tests/README.md` |
| Vitest config | `apps/studio/vitest.config.ts` |
| Related skills | `studio-e2e-tests` (running E2E), `vitest` (API reference), `vercel-composition-patterns` (component architecture) |
章节与文件索引
按说明文件中的顺序排列。
何时申请
按优先级划分的规则类别
1. 逻辑提取(关键)
2. 测试覆盖率(关键)
3. 组件测试(高)
文件/命令 -> apps/studio/ · testing- · testing-extract-logic · .utils.ts · testing-exhaustive-permutations · testing-component-tests-ui-only · testing-e2e-shared-features · ComponentName.utils.ts
内容校验值 -> bd0cddecb383
当前原文的自动摘译,尚未人工审校。
何时申请
在以下情况下请参考这些准则: 为 Studio 代码编写新测试 决定编写哪种类型的测试(单元、组件、E2E) 从组件中提取逻辑以使其可测试 检查测试覆盖率是否足够
按优先级划分的规则类别
优先级 · 类别 · 影响 · 前缀 1 · 逻辑提取 · 关键 ·
testing-2 · 测试覆盖率 · 关键 ·testing-3 · 组件测试 · 高 ·testing-4 · E2E 测试 · 高 ·testing-1. 逻辑提取(关键)
testing-extract-logic- 将组件中的逻辑作为纯函数删除到.utils.ts文件中:参数输入,返回2. 测试覆盖率(关键)
testing-exhaustive-permutations- 测试效用函数的每个排列:快乐路径、格式错误的输入、空值、边缘情况3. 组件测试(高)
testing-component-tests-ui-only- 只为复杂的UI交互逻辑编写组件测试,而不是业务逻辑