# Writing Tests

> Guides test creation with practical strategies for unit, integration, and e2e tests. Use when writing tests, deciding what to test, setting up test infrastructure, or discussing coverage. Triggers on "write tests", "should I test", "test coverage", or test file creation. Use when this capability is needed.

- Skill: `tomevault-io/writing-tests-4` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/writing-tests-4`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/writing-tests-4/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/writing-tests-4

---


# Writing Tests

Test behavior, not implementation. Prioritize by risk.

## When to Use Each Type

| Type | What | When | Tools |
|------|------|------|-------|
| Unit | Single function/hook | Pure logic, calculations | Vitest, Jest |
| Integration | Component + deps | Forms, data display | Testing Library |
| E2E | Full user flow | Critical paths | Playwright |

## Always Test

- Auth flows
- Payment/checkout
- Data mutations (create, update, delete)
- Authorization rules
- Critical business logic

## Rarely Test

- Static content
- Third-party internals
- Pure styling

## File Organization

```
src/components/Button/
├── Button.tsx
└── Button.test.tsx      # Colocated

e2e/                     # E2E at root
├── auth.spec.ts
└── checkout.spec.ts
```

## Patterns

### Arrange-Act-Assert

```typescript
test('increments on click', () => {
  // Arrange
  render(<Counter initial={0} />)
  // Act
  fireEvent.click(screen.getByRole('button'))
  // Assert
  expect(screen.getByText('1')).toBeInTheDocument()
})
```

### Query Priority (best → worst)

1. `getByRole` - semantic, accessible
2. `getByLabelText` - form inputs
3. `getByText` - content
4. `getByTestId` - last resort

### Test Behavior, Not Implementation

```typescript
// ❌ Implementation
expect(component.state.isOpen).toBe(true)

// ✅ Behavior
expect(screen.getByRole('dialog')).toBeVisible()
```

## Minimum Viable Testing

If time-constrained:
1. E2E for happy paths (covers most ground)
2. Unit for complex logic (catches edge cases)
3. Integration for critical components (forms, data)

## Advanced Patterns

For API mocking, test fixtures, and coverage guidance, see [MOCKING.md](MOCKING.md).

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/augmnt) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-11 -->

