Write automated tests following TDD principles: red-green-refactor, vertical slices, behavior over implementation. Consult this skill whenever creating tests, adding test coverage, writing test-first code, fixing a bug with a regression test, or any task that involves verifying behavior through automated tests.
Resources
1Install
npx skillscat add kvnwolf/devtools/create-test Install via the SkillsCat registry.
Testing
Write automated tests following TDD principles.
Philosophy
Based on Matt Pocock's TDD approach:
- Test behavior through public interfaces, not implementation details
- Good tests survive refactors — if the public API doesn't change, tests shouldn't break
- Mock only at system boundaries (external APIs, databases, time/randomness)
- Never mock your own modules or internal collaborators
Workflow
Vertical Slices (Not Horizontal)
Wrong approach (horizontal): write ALL tests first, then implement everything.
Correct approach (vertical): write ONE test → make it pass → repeat.
Each cycle:
- Write a single test that describes expected behavior
- Run it and confirm it fails (red)
- Write the minimum code to make it pass (green)
- Refactor if needed (refactor)
- Move to next behavior
Planning Phase
Before writing tests:
- List the behaviors to verify (what should happen, not how)
- Order them from simplest to most complex
- Start with the "tracer bullet" — the simplest end-to-end case
Convention
Co-locate tests next to source: foo.test.ts next to foo.ts.
Vitest API
import { describe, expect, test, vi } from "vitest";test("description", () => { ... })— Define a testdescribe("group", () => { ... })— Group related testsexpect(value).toBe(expected)— Strict equalityexpect(value).toEqual(expected)— Deep equalityexpect(fn).toThrow()— Assert throwsvi.fn()— Create a mock functionvi.mock("module")— Mock a module
Checklist Per Cycle
Before moving to the next test, verify:
- Test describes behavior, not implementation
- Test uses the public interface
- Test would survive an internal refactor
- Implementation is the minimum needed to pass
- No speculative features added
For mocking guidelines, see references/mocking.md.
For good vs bad test examples, see references/examples.md.