Test Structure
Tests are organized in thetest/ directory:
Running Tests
All Tests
Unit Tests
Smoke Tests
Smoke tests are integration tests that verify critical user flows:Watch Mode
Run tests in watch mode during development:Coverage
Generate code coverage report:coverage/ directory.
Coverage thresholds (from vitest.config.ts):
Interactive UI
Run tests with interactive UI:Test Configuration
Configuration is invitest.config.ts:
Setup Files
- Unit tests:
test/unit/setup.ts- Mocks native APIs, IndexedDB, DOM - Smoke tests:
test/smoke/setup.ts- Mocks Tauri APIs, browser APIs
Writing Unit Tests
Basic Test Structure
Testing Async Code
Testing Errors
Testing React Components
Mocking
Mock Functions
Mock Modules
Mock Implementation
Test Utilities
Located intest/unit/setup.ts:
createMockApp
createMockManifest
waitFor
createDeferred
Writing Smoke Tests
Smoke tests verify critical user workflows:Testing Guidelines
Test Naming
Test Independence
Each test should be independent:Test Coverage
What to test:- ✅ Public APIs
- ✅ Edge cases
- ✅ Error handling
- ✅ Complex logic
- ✅ User workflows (smoke tests)
- ❌ Private methods directly (test through public API)
- ❌ Third-party libraries
- ❌ Trivial getters/setters
- ❌ Implementation details
Arrange-Act-Assert Pattern
Continuous Integration
Tests run automatically on CI:Debugging Tests
Run Single Test
Debug with VS Code
Add to.vscode/launch.json:
Use console.log
Common Testing Patterns
Testing Event Emitters
Testing Timers
Testing localStorage
Testing IndexedDB
Best Practices
-
Run tests before committing
-
Write tests for bug fixes
- Add test that reproduces the bug
- Fix the bug
- Verify test passes
-
Keep tests fast
- Mock external dependencies
- Avoid unnecessary delays
- Use fake timers
-
Test behavior, not implementation
-
Use descriptive test names
Summary
- Use Vitest for all tests
- Separate unit tests (fast) from smoke tests (slow)
- Run
bun run testbefore committing - Aim for >50% code coverage
- Write tests for bug fixes
- Keep tests independent and fast
- Use mocks for external dependencies
- Follow AAA pattern (Arrange-Act-Assert)
