AgentSkillsCN

tdd

所有实现工作均以此为默认标准。触发关键词包括:“实现”、“构建”、“新增功能”、“修复 Bug”、“编写代码”,以及任何编码任务的启动。铁律:未经测试先失败,绝不允许向生产环境提交代码。唯有在明确的原型开发场景下,方可跳过测试环节。

SKILL.md
--- frontmatter
name: tdd
description: "DEFAULT for all implementation. Triggers: 'implement', 'build', 'add feature', 'fix bug', 'write code', starting any coding task. Iron law: no production code without failing test first. Skip only for explicit prototypes."

Test-Driven Development

Write test first → watch fail → minimal code → pass → refactor.

Iron Law: No production code without failing test first. Violate → delete code, start over.

Red-Green-Refactor Cycle

RED - Write Failing Test

One minimal test showing desired behavior.

typescript
test('rejects empty email', async () => {
  const result = await submitForm({ email: '' });
  expect(result.error).toBe('Email required');
});

Requirements: one behavior, clear name, real code (mocks only if unavoidable).

Verify RED

MANDATORY. Never skip.

bash
npm test path/to/test.test.ts

Confirm:

  • Test fails (not errors)
  • Failure = feature missing (not typo)

Test passes? Testing existing behavior. Fix test.

GREEN - Minimal Code

Simplest code to pass. No extra features, no refactoring yet.

Verify GREEN

MANDATORY.

  • Test passes
  • Other tests still pass
  • Output pristine (no warnings)

REFACTOR

After green only: remove duplication, improve names, extract helpers. Keep tests green.

Repeat

Next failing test → next feature.

When to Use

Always: new features, bug fixes, refactoring, behavior changes.

Exceptions (ask human): throwaway prototypes, generated code, config files.

Common Rationalizations

ExcuseReality
"Too simple to test"Simple code breaks. Test takes 30s.
"I'll test after"Tests passing immediately prove nothing.
"Already manually tested"Ad-hoc != systematic. Can't re-run.
"Deleting X hours is wasteful"Sunk cost. Unverified code = tech debt.
"Keep as reference"You'll adapt it = testing after. Delete means delete.
"Need to explore first"Fine. Throw away exploration, TDD from scratch.
"Test hard = skip TDD"Hard to test = hard to use. Simplify design.
"TDD slows me down"TDD faster than debugging production.
"Existing code has no tests"Improving it? Add tests for touched code.

Verification Checklist

Before marking complete:

  • Every new function has test
  • Watched each test fail before implementing
  • Each test failed for expected reason
  • Wrote minimal code to pass
  • All tests pass
  • Output pristine
  • Real code tested (minimal mocks)
  • Edge cases + errors covered

Can't check all? Skipped TDD. Start over.

Red Flags - STOP and Restart

  • Code before test
  • Test passes immediately
  • Can't explain why test failed
  • "Just this once"
  • "Keep as reference"
  • "Already spent X hours"
  • "This is different because..."

All mean: delete code, start over with TDD.

When Stuck

ProblemSolution
Don't know how to testWrite wished-for API. Ask human.
Test too complicatedDesign too complicated. Simplify.
Must mock everythingCode too coupled. Use DI.
Test setup hugeExtract helpers or simplify design.

Bug Fixes

Bug found → write failing test reproducing it → TDD cycle → test proves fix + prevents regression.

Never fix bugs without test.

See Also

  • debugging skill → root cause investigation before fixing
  • verification-before-completion → evidence before claiming done