Skill站外收录 @skilllib/skill-2117939930272411464
s2-unit-test
规范 S2 项目单元测试的编写与维护流程,涵盖测试位置选择、真实源码调用、行为验证及缺陷复现等核心实践,确保代码修改具备充分的行覆盖、分支覆盖和回归防护能力。
测试与安全 AI 技能
Skills.MD
# S2 Unit Testing Guidelines
## When to Use This Skill
Use this skill when you:
- Modify code under `packages/*/src/`
- Fix bugs (especially with issue numbers)
- Add new features or functions
**By default, all code changes require corresponding unit tests.**
## Test File Location Strategy
### Step 1: Find Existing Test Files
Search for `__tests__` directories to find where tests for the modified file already exist:
```
packages/s2-core/__tests__/unit/ # Unit tests organized by module
packages/s2-core/__tests__/bugs/ # Bug regression tests with issue numbers
packages/s2-core/__tests__/spreadsheet/ # Integration-level spreadsheet tests
packages/s2-react/__tests__/ # React component tests
packages/s2-vue/__tests__/ # Vue component tests
```
### Step 2: Choose the Right Location
| Scenario | Location | File Naming |
|----------|----------|-------------|
| Modifying existing function | Add to existing test file for that function | N/A |
| Bug fix with issue number | `packages/s2-core/__tests__/bugs/` | `issue-{number}-spec.ts` |
| New utility function | `packages/s2-core/__tests__/unit/utils/` | `{function-name}-spec.ts` |
| New cell logic | `packages/s2-core/__tests__/unit/cell/` | `{cell-type}-spec.ts` |
| New interaction | `packages/s2-core/__tests__/unit/interaction/` | `{interaction-name}-spec.ts` |
**Prefer adding tests to existing files over creating new ones.** Reuse existing helper functions and test utilities.
## Critical Rules
### ✅ Good Practices
1. **Import from `src` directory** - Tests must exercise actual source code:
```typescript
// Good: Import functions/classes from src
import { getCellWidth, getDisplayText } from '@/utils/text';
import { PivotSheet } from '@/sheet-type';
// Good: Use path aliases
import { createPivotSheet } from 'tests/util/helpers';
```
2. **Test real behavior** - Create actual instances and verify logic:
```typescript
// Good: Create real S2 instance and test behavior
const s2 = createPivotSheet(options);
await s2.render();
expect(s2.facet.getColCells()[0].getMeta().width).toBe(expectedWidth);
```
3. **Reproduce bugs with real code paths**:
```typescript
// Good: Bug reproduction that exercises src code
describe('issue #3212', () => {
test('should keep column width after hiding value', async () => {
const s2 = createPivotSheet({ style: { layoutWidthType: 'compact' } });
await s2.render();
const originalWidth = s2.facet.getColCells()[0].getMeta().width;
s2.setOptions({ style: { colCell: { hideValue: true } } });
await s2.render();
expect(s2.facet.getColCells()[0].getMeta().width).toBe(originalWidth);
});
});
```
### ❌ Bad Practices
1. **Never reimplement logic in tests**:
```typescript
// BAD: Reimplementing the logic defeats the purpose
function myLocalCalculation(a, b) {
return a + b; // This is useless! If src is broken, test still passes
}
expect(myLocalCalculation(1, 2)).toBe(3);
```
2. **Never import only types**:
```typescript
// BAD: Only importing types doesn't test any actual code
import type { S2Options, SpreadSheet } from '@/common';
// No actual code is being tested!
```
3. **Never test mocked implementations instead of real code**:
```typescript
// BAD: Testing your own mock, not the actual source
const mockFn = jest.fn().mockReturnValue(42);
expect(mockFn()).toBe(42); // This tests nothing useful
```
## Test Structure Template
```typescript
/**
* @description spec for issue #XXXX (if applicable)
* https://github.com/antvis/S2/issues/XXXX
*/
import { SomeFunction, SomeClass } from '@/path/to/src';
import { createPivotSheet } from 'tests/util/helpers';
describe('FeatureName', () => {
test('should do expected behavior', () => {
// Arrange
const input = { /* ... */ };
// Act
const result = SomeFunction(input);
// Assert
expect(result).toEqual(expectedOutput);
});
});
```
## Running Tests
```bash
# Run all tests for a package
pnpm --filter @antv/s2 test
# Run specific test file
pnpm --filter @antv/s2 test -- --testPathPattern="issue-3212"
# Run with coverage
pnpm --filter @antv/s2 test:coverage
```
## Goal
The primary goal of unit tests is to:
- **Increase line coverage** - Every line of src code should be exercised
- **Increase branch coverage** - Test all conditional paths
- **Prevent regressions** - Ensure bugs don't reappear
Tests that don't import and exercise actual `src` code provide no coverage benefit.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「s2-unit-test」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次