What to Unit Test
Focus on business logic and utility functions:Billing Calculations
Value-based billing formula, day rates, prorated revenue
Time Utilities
Working days, t-shirt sizing, time formatting
Data Transformations
Parsing, validation, formatting functions
Helper Functions
Pure functions with deterministic output
Test Structure
File Organization
Basic Test Example
Vitest Features
Test Blocks
Assertions
Async Tests
Common Testing Patterns
Testing Pure Functions
Testing Date Functions
Testing Complex Calculations
Testing with Edge Cases
Running Tests
Basic Commands
Watch Mode
Watch mode automatically reruns tests when files change:- Press
ato run all tests - Press
fto run only failed tests - Press
tto filter by test name - Press
pto filter by filename - Press
qto quit
Test Coverage
Generating Coverage Reports
Coverage Configuration
Coverage Thresholds
Target coverage:- Business logic: 80%+
- Utilities: 90%+
- Components: Not required
Mocking
Mocking Functions
Mocking Modules
Best Practices
Test Behavior, Not Implementation
Test Behavior, Not Implementation
Focus on what the function does, not how:
Use Descriptive Test Names
Use Descriptive Test Names
Test names should describe the scenario:
Keep Tests Independent
Keep Tests Independent
Tests should not depend on each other:
Test Edge Cases
Test Edge Cases
Don’t just test the happy path:
Debugging Tests
Using Console
Using Vitest UI
Debugging in VS Code
Add to.vscode/launch.json: