augmnt/webdev-skills

writing-tests

Guides test creation with practical strategies for unit, integration, and e2e tests.

查看源码
仓库原始内容

按源仓库内容呈现,保留标题、案例、代码、表格、链接以及原文引用的演示图片。

Writing Tests

Test behavior, not implementation. Prioritize by risk.

When to Use Each Type

TypeWhatWhenTools
UnitSingle function/hookPure logic, calculationsVitest, Jest
IntegrationComponent + depsForms, data displayTesting Library
E2EFull user flowCritical pathsPlaywright

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.

来自同一仓库

更多 Skills

全部 Skills