Testing Guide
Testing Guide
Last Updated: 2025-12-04
Coverage Goal: 70%+ overall, 80%+ for critical paths
Overview
This guide covers testing strategies, patterns, and best practices for the Synap backend. We use Vitest for all testing with real PostgreSQL databases for integration tests.
Quick Start
Running Tests
Test Infrastructure
Vitest Configuration
Each package has a vitest.config.ts that auto-loads environment variables:
Environment Files
.env.test (auto-loaded):
Test Data Management
Convention: test-* Prefix
All test data must use test- prefix for user IDs:
Cleanup Pattern
Test Utilities
Helper Functions
packages/database/src/__tests__/test-utils.ts:
Repository Testing
Example: EventRepository Tests
Service Testing (with Mocks)
Example: VectorService with Mocked Embeddings
Testing Inngest Workers
Overview
Synap uses Inngest workers for 3-phase event processing. Testing workers requires:
- Mocking Inngest events
- Testing permission validation logic
- Verifying DB operations
- Testing event emissions
Example: Permission Validator Worker
Worker code:
Test:
Testing CRUD Workers
Worker code:
Test:
Testing 3-Phase Flow
Integration test:
Testing Patterns
1. User Isolation
Always test that users can't access each other's data:
2. Data Serialization
Test that complex data types are handled correctly:
3. Edge Cases
Always test boundaries and edge cases:
4. Concurrent Operations
Test concurrent writes:
Coverage Goals
Critical Paths (80%+ coverage)
- EventRepository
- VectorRepository
- EntityRepository
- Core domain services
Standard Paths (70%+ coverage)
- API routes
- Business logic
- Utilities
Lower Priority (50%+ coverage)
- UI components
- Admin tools
- Dev utilities
Best Practices
1. Import Test Globals Explicitly
2. Use Descriptive Test Names
3. One Assertion Per Test (When Possible)
4. Clean Up Properly
Common Pitfalls
❌ Not Using test- Prefix
❌ Forgetting to Close Connections
❌ Testing with Real API Keys
❌ Not Cleaning Between Tests
Running Tests in CI/CD
GitHub Actions Example
Debugging Tests
Enable Debug Logging
Run Single Test
Use Console Logging
Check Database State
Resources
- Vitest Docs: https://vitest.dev
- Test Examples:
packages/database/src/__tests__/ - Test Utilities:
packages/database/src/__tests__/test-utils.ts - Error Report:
error_resolution_report.md- Common test issues
Summary
Key Takeaways:
- Always use
test-prefix for test data - Clean up before and after tests
- Test user isolation
- Mock external APIs
- Test edge cases (empty, large, special chars)
- Keep tests independent
- Aim for 70%+ coverage
Next Steps:
- Review existing tests as examples
- Write tests for new features
- Run coverage reports regularly
- Fix failing tests immediately
