基于 SOC 职业分类
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/carrot-foundation/schemas --skill rule-testing命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
Schema version injection, $id format, and SCHEMA_VERSION environment variable
Generated JSON Schema structure — required fields, validation patterns, and $ref usage
Use when a task is complete and needs the full check, commit, and PR workflow
| name | rule-testing |
| description | Vitest patterns, 100% coverage thresholds, and fixture-driven schema validation |
Apply this rule whenever work touches:
src/**/*.spec.tssrc/test-utils/**The schemas package enforces 100% test coverage. Every schema, utility, and export must be thoroughly tested because this is a published library — bugs propagate to every consumer.
Tests live in __tests__/ directories adjacent to the source code they test:
src/
mass-id/
__tests__/
mass-id.data.schema.spec.ts
mass-id.schema.spec.ts
mass-id.data.schema.ts
mass-id.schema.ts
shared/
schemas/
primitives/
__tests__/
ids.schema.spec.ts
ids.schema.ts
Always use the .spec.ts extension. Never use .test.ts — the project is configured for .spec.ts only.
Coverage thresholds are enforced at 100% for all metrics:
If coverage drops, add tests. Never reduce thresholds or delete tests to meet them.
Use centralized fixtures from src/test-utils/fixtures/ for valid data. Fixtures follow these conventions:
// src/test-utils/fixtures/mass-id-data.fixture.ts
export const MassIDDataFixture: MassIDData = {
type: 'Mass ID',
name: 'Test Mass ID',
weight_kg: 1500,
// ... all required fields with valid values
};
When tests need variations of valid data, use factory functions:
export function createMassIDDataFixture(
overrides?: Partial<MassIDData>,
): MassIDData {
return {
...MassIDDataFixture,
...overrides,
};
}
Before creating local test data, check src/test-utils/fixtures/ for existing fixtures. Import and override rather than duplicating.
.safeParse() for validation testsAlways use .safeParse() instead of .parse() in tests. This allows explicit assertions on both success and failure cases without try/catch.
// GOOD: safeParse for success
it('should accept valid mass-id data', () => {
const result = MassIDDataSchema.safeParse(MassIDDataFixture);
expect(result.success).toBe(true);
expect(result.data).toEqual(MassIDDataFixture);
});
// GOOD: safeParse for failure
it('should reject missing required field', () => {
const { name, ...withoutName } = MassIDDataFixture;
const result = MassIDDataSchema.safeParse(withoutName);
expect(result.success).toBe(false);
expect(result.error?.issues).toEqual(
expect.arrayContaining([expect.objectContaining({ path: ['name'] })]),
);
});
Every schema test suite must cover:
describe('LocationSchema', () => {
it('should accept valid complete location', () => { ... });
it('should accept location with only required fields', () => { ... });
it('should reject invalid latitude type', () => { ... });
it('should reject missing longitude', () => { ... });
it('should reject extra properties', () => { ... });
it('should reject latitude outside range [-90, 90]', () => { ... });
it('should accept optional description when present', () => { ... });
});
it.eachUse it.each for testing multiple inputs against the same assertion. This is especially useful for enum validations, boundary checks, and field type validations.
it.each([
['valid-uuid', true],
['not-a-uuid', false],
['', false],
['123e4567-e89b-12d3-a456-426614174000', true],
])('should validate UUID "%s" as %s', (input, expected) => {
const result = UuidSchema.safeParse(input);
expect(result.success).toBe(expected);
});
it.each([
['None', true],
['Low', true],
['Medium', true],
['High', true],
['none', false],
['NONE', false],
['Invalid', false],
])('should validate contamination level "%s" as %s', (input, expected) => {
const result = ContaminationLevelSchema.safeParse(input);
expect(result.success).toBe(expected);
});
Verify that Zod's inferred types match expected TypeScript types using expectTypeOf from Vitest:
import { expectTypeOf } from 'vitest';
it('should infer correct type from MassIDDataSchema', () => {
type Inferred = z.infer<typeof MassIDDataSchema>;
expectTypeOf<Inferred>().toMatchTypeOf<MassIDData>();
});
Follow the Arrange-Act-Assert pattern:
it('should reject weight below minimum', () => {
// Arrange
const input = createMassIDDataFixture({ weight_kg: -1 });
// Act
const result = MassIDDataSchema.safeParse(input);
// Assert
expect(result.success).toBe(false);
});
.only — committed tests must never use it.only or describe.onlyexpect(true).toBe(true) — always assert meaningful values