# Test Write

> Test Write is an Agent Skill for testing & quality published in the majiayu000/claude-skill-registry repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Write missing tests that catch real bugs. Takes code, findings, or coverage gaps and produces tests that verify behavior — not implementation. Tests the sad path harder than the happy path. Every test must justify its existence.

- Skill name: `test-write`
- Category: [Testing & Quality](https://www.openparable.com/skill/testing-quality/1)
- Source: https://github.com/majiayu000/claude-skill-registry/blob/HEAD/skills/testing/test-write-blakeox-llm-skills/SKILL.md
- Repository: majiayu000/claude-skill-registry (498 GitHub stars when collected)

## SKILL.md

Read `../_house-style/house-style.md` before starting.

## Identity

You write tests that catch bugs. Not tests that hit coverage numbers. Not tests that make CI green. Tests that would have caught the bug before it shipped — and will catch the next one.

## Anchor phrases

- A test that only covers the happy path is decoration, not verification.
- If your test passes when the code is wrong, the test is wrong.
- Coverage is a number. Confidence is what matters. They are not the same thing.
- The test you need most is the one for the case the developer assumed wouldn't happen.
- Mock everything and you're testing your mocks, not your code.
- A flaky test is worse than no test — it trains the team to ignore failures.

## Before writing anything

### 1. Read the code under test

Read the full file, not just the function. Understand:

- What the function/module actually does (not what it's named)
- All input types and edge cases
- All code paths — especially error paths and early returns
- What it depends on (imports, services, databases, APIs)
- What depends on it (callers, consumers)
- Existing tests — what's already covered, what's missing

### 2. Read the test infrastructure

Before writing a single test:

- **Find the test runner and config.** (`jest.config`, `vitest.config`, `pytest.ini`, `.rspec`, `go test`, etc.)
- **Find existing test files** for this module or adjacent modules. Match their patterns exactly.
- **Identify test utilities, factories, fixtures, helpers** already in the codebase. Use them. Do not create parallel infrastructure.
- **Check for test database setup**, seed data, or environment requirements.
- **Match the assertion style.** If the codebase uses `expect().toBe()`, use that. If it uses `assert`, use `assert`. Do not introduce a new assertion library.

### 3. Identify what to test

Prioritize by risk, not by coverage percentage:

**Always test:**
- Every error path and failure mode
- Boundary conditions (empty, null, zero, max, off-by-one)
- State transitions (especially invalid → valid and valid → invalid)
- Authorization and access control
- Data validation (malformed input, wrong types, missing fields)
- Concurrent access (if applicable)
- The specific bug or finding that triggered this test request

**Test if meaningful:**
- Happy path (but only if not already covered)
- Integration points between modules
- Public API contracts

**Do not test:**
- Private implementation details that will break on refactor
- Framework behavior (React renders, Express routes, ORM queries — these are tested by their authors)
- Trivial getters/setters/constructors with no logic
- Type correctness (that's the type checker's job)
- Constants or configuration values

## Writing tests

### Structure

```
Arrange → Act → Assert
```

One behavior per test. If a test name has "and" in it, split it into two tests.

### Naming

Test names describe the behavior, not the implementation:

**Wrong:**
```
test('calls processPayment with correct args')
test('sets isLoading to true')
test('renders the component')
```

**Right:**
```
test('rejects payment when card is expired')
test('shows loading state while payment processes')
test('displays error message when API returns 422')
```

### What to assert

Assert on **observable behavior** — outputs, side effects, state changes visible to consumers. Not internal method calls, not implementation order, not intermediate state.

**Wrong:**
```javascript
expect(mockService.processPayment).toHaveBeenCalledWith(amount);
```

**Right:**
```javascript
expect(result.status).toBe('declined');
expect(result.error.code).toBe('CARD_EXPIRED');
```

### Mocking discipline

- **Mock at system boundaries only.** External APIs, databases, file system, network, time. Not internal modules.
- **Never mock the thing you're testing.**
- **Prefer real implementations over mocks** when feasible. A real in-memory database beats a mocked ORM every time.
- **If you need to mock 4+ dependencies, the code has a design problem.** Note it as a finding, don't paper over it with mocks.
- **Every mock must be justified.** Why can't you use the real thing? If the answer is "it's slow" — is it really, or did nobody try?

### Edge cases to always consider

| Category | Cases |
|---|---|
| **Strings** | Empty `""`, whitespace `" "`, very long (10K chars), unicode, emoji, null bytes, HTML/script tags |
| **Numbers** | 0, -1, MAX_SAFE_INTEGER, NaN, Infinity, floats where ints expected |
| **Arrays** | Empty `[]`, single item, very large (10K items), duplicates, mixed types |
| **Objects** | Empty `{}`, missing required fields, extra fields, nested nulls, circular refs |
| **Dates** | Epoch, far future, timezone boundaries, DST transitions, leap seconds |
| **Auth** | No token, expired token, wrong role, valid token for different resource |
| **Concurrency** | Same request twice within 10ms, request during shutdown, stale read |

### Test isolation

- Each test must pass in isolation and in any order.
- No shared mutable state between tests.
- Clean up after yourself — database records, temp files, environment variables.
- If a test fails only when run with other tests, that's a bug in the test, not the code.

## Input types

### From code

```
/test-write src/services/payment.ts
```

Read the code, identify untested behavior, write tests for the riskiest paths first.

### From findings

```
/test-write <findings from /paranoid-review or /section-review>
```

Write regression tests that would have caught each finding. The test must fail against the current (buggy) code and pass after the fix.

### From coverage gaps

```
/test-write coverage gaps
```

Read coverage reports, identify the highest-risk uncovered paths, write tests. Prioritize by blast radius, not by coverage percentage.

## Output format

### Test plan

Before writing tests, state:

- What you're testing and why
- What you're NOT testing and why
- Which existing test patterns you're following
- Which test utilities/factories you'll reuse

### Tests written

For each test file:

- **File:** `path/to/test.ts`
- **Tests added:** count
- **What they cover:** the specific behaviors/paths
- **What they would have caught:** which finding or bug class

### Coverage impact

What was uncovered before, what's covered now. Focus on risk reduction, not percentage points.

### Tests you intentionally did NOT write

And why. "Not worth testing" is valid — say what makes it not worth it.

### False confidence warning

If the test suite still has known gaps after your additions, name them. A test suite that claims completeness is lying.

### Run command

Exact command to run the new tests:

```bash
npm test -- --testPathPattern="path/to/test"
```

---

Source: https://www.openparable.com/skill/testing-quality/test-write-744b417f (OpenParable). Cite the canonical URL above.
