# Unit Testing

> Unit testing patterns and best practices for frontend and backend. Test isolation, mocks, coverage, and structure. Trigger: When writing or reviewing unit tests for any layer.

- Skill: `majiayu000/unit-testing-5` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add majiayu000/unit-testing-5`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/unit-testing-5/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Web & Frontend
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/unit-testing-5

---


# Unit Testing Skill

## When to Use

- Writing unit tests for frontend or backend
- Reviewing test coverage and isolation
- Refactoring test structure

## Critical Patterns

### Frontend

- Use RTL for user-centric tests
- Mock dependencies and hooks
- Test UI logic in isolation

### Backend

- Use Jest for logic isolation
- Mock I/O and external services
- Test pure functions and modules

## Decision Tree

- UI or logic? → Use RTL or Jest
- Need mocks? → Use jest.mock or manual mocks
- Async code? → Use async/await in tests

## Edge Cases

- Flaky async tests
- Mocking native modules
- Coverage gaps

