unit-testing
Installation
SKILL.md
Unit Testing — Testing Isolated Functions and Classes
Overview
Unit tests verify the smallest testable parts of an application in isolation. They are fast, cheap to run, and provide rapid feedback on whether individual functions, methods, and classes behave correctly. In the Test Trophy model, unit tests sit above static analysis and below integration tests.
When to unit test: Pure functions, business logic, algorithms, data transformations, edge cases, error handling, and any code with complex branching.
The AAA Pattern (Arrange-Act-Assert)
Every unit test should follow three distinct phases:
- Arrange — Set up test data, dependencies, and preconditions
- Act — Execute the function or method under test
- Assert — Verify the result matches expectations
AAA in TypeScript (Vitest)
import { describe, it, expect } from 'vitest';
import { calculateDiscount } from './pricing';