Unit Testing
Unit Testing
Section titled “Unit Testing”Introduction
Section titled “Introduction”Unit tests verify individual functions, utilities, and components in isolation. They’re fast to run and easy to write.
Testing Utilities
Section titled “Testing Utilities”export function formatDate(date: Date): string { return new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric', }).format(date)}
export function capitalize(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1)}import { describe, it, expect } from 'vitest'import { formatDate, capitalize } from '@/lib/format'
describe('formatDate', () => { it('formats a date correctly', () => { const date = new Date('2024-01-15') expect(formatDate(date)).toBe('January 15, 2024') })})
describe('capitalize', () => { it('capitalizes the first letter', () => { expect(capitalize('hello')).toBe('Hello') })
it('handles empty strings', () => { expect(capitalize('')).toBe('') })})Testing Components
Section titled “Testing Components”export function Greeting({ name }: { name: string }) { return <h1>Hello, {name}!</h1>}import { render, screen } from '@testing-library/react'import { Greeting } from '@/components/Greeting'
describe('Greeting', () => { it('renders the name', () => { render(<Greeting name="Alice" />) expect(screen.getByText('Hello, Alice!')).toBeDefined() })})Running Tests
Section titled “Running Tests”# Run all testsnpx vitest
# Run with watch modenpx vitest --watch
# Run once (CI)npx vitest run
# Check coveragenpx vitest run --coverageCommon Mistakes
Section titled “Common Mistakes”- Testing implementation details — Test behavior, not internal state or methods.
- Too many mocks — If you need to mock everything, the code might be too coupled.
- Not testing edge cases — Empty strings, null values, error states.
Best Practices
Section titled “Best Practices”- Test behavior, not implementation
- One assertion per test (or a few related ones)
- Use descriptive test names that explain the expected behavior
- Run tests on every commit (pre-commit hook or CI)
Summary
Section titled “Summary”Unit tests verify individual functions and components. They’re fast and should run on every commit. Test behavior, not implementation. Cover edge cases and error states.