Testing Guide
Excalidraw uses comprehensive testing to ensure code quality and prevent regressions. This guide covers testing practices, tools, and workflows.Testing Framework
Vitest
Excalidraw uses Vitest as its test framework:- Fast: Extremely fast test execution
- Vite-powered: Uses Vite’s transformation pipeline
- Jest-compatible: Compatible with Jest API and matchers
- TypeScript: First-class TypeScript support
Testing Libraries
- @testing-library/react - React component testing
- @testing-library/jest-dom - Custom DOM matchers
- vitest-canvas-mock - Canvas API mocking
- fake-indexeddb - IndexedDB mocking for persistence tests
Running Tests
Basic Commands
Running Specific Tests
Running All Test Suites
yarn test:typecheck- TypeScript type checkingyarn test:code- ESLint code quality checksyarn test:other- Prettier formatting checksyarn test:app --watch=false- All unit/integration tests
Test Coverage
Coverage Requirements
Excalidraw enforces minimum coverage thresholds:Viewing Coverage
Coverage Reports
Generated incoverage/ directory:
index.html- Interactive HTML reportlcov.info- LCOV format for CI toolscoverage-summary.json- JSON summary
Writing Tests
Test File Location
Place test files next to the code they test:tests/ directory:
Test File Naming
*.test.ts- Unit tests for utilities*.test.tsx- Component tests*.spec.ts- Spec/integration tests__snapshots__/- Snapshot files (auto-generated)
Basic Test Structure
Testing Patterns
Unit Testing Utilities
Testing React Components
Testing with Jotai Atoms
Testing Async Code
Snapshot Testing
Testing Canvas Operations
Canvas operations are mocked byvitest-canvas-mock:
Mocking
Mocking Functions
Mocking Modules
Mocking Timers
Test Organization
Describe Blocks
Group related tests:Setup and Teardown
Testing Best Practices
Do’s
- Test Behavior, Not Implementation: Focus on what code does, not how
- Write Descriptive Test Names: Use “should” statements
- Follow AAA Pattern: Arrange, Act, Assert
- Test Edge Cases: Include boundary conditions and error cases
- Keep Tests Independent: Each test should run in isolation
- Use Meaningful Assertions: Be specific about expected outcomes
- Test User Interactions: Simulate real user behavior
- Mock External Dependencies: Isolate the code under test
Don’ts
- Don’t Test Implementation Details: Test the public API
- Don’t Write Brittle Tests: Avoid testing exact HTML structure
- Don’t Skip Error Cases: Always test error handling
- Don’t Test Third-Party Code: Trust external libraries
- Don’t Use Magic Numbers: Define constants for test data
- Don’t Share State Between Tests: Reset state in hooks
- Don’t Over-Mock: Mock only what’s necessary
Debugging Tests
Run Tests in Debug Mode
Use Test UI
- Browse and run specific tests
- View test results and coverage
- Debug test failures
Console Logging
Debug in VS Code
Add to.vscode/launch.json:
Continuous Integration
Pre-commit Checks
Tests run automatically via Husky pre-commit hook:Before Committing
Always run before committing:CI Workflow
GitHub Actions runs these checks on PRs:- Type checking:
yarn test:typecheck - Linting:
yarn test:code - Formatting:
yarn test:other - Tests:
yarn test:app --watch=false - Coverage: Uploads coverage reports
Common Testing Scenarios
Testing Element Operations
Testing Keyboard Shortcuts
Testing Collaboration Features
Resources
Next Steps
Now that you understand testing:- Review the Contribution Guidelines for code standards
- Find an issue on the roadmap
- Write tests for your changes before submitting PRs
- Join Discord if you need help with testing