Testing Strategy
Better Uptime uses two test runners:- Vitest - For most packages (validators, etc.)
- Bun Test - For API packages using Bun runtime
Running Tests
Run All Tests
From the root of the monorepo:Watch Mode
Run tests in watch mode for active development:Coverage Reports
Generate test coverage reports:coverage/ directory of each package.
Package-Specific Testing
Testing with Vitest
Used by:@repo/validators
Configuration
packages/validators/vitest.config.ts:
Package Scripts
Run Vitest Tests
Example Test
Testing with Bun
Used by:@repo/api
Package Scripts
Run Bun Tests
Example Test
Workspace Test Configuration
The rootvitest.workspace.ts defines which packages use Vitest:
@repo/api.
Test Organization
Directory Structure
Tests are organized in__tests__ directories:
Test Naming
- Unit tests:
*.test.ts - Integration tests:
*.integration.test.ts - E2E tests:
*.e2e.test.ts
Coverage Configuration
Vitest Coverage
Coverage is provided by@vitest/coverage-v8:
- Text summary (console)
- JSON report
- HTML report (browsable)
Coverage Exclusions
Exclude test files from coverage:Turborepo Test Pipeline
Test Task Configuration
turbo.json configures test behavior:
- Tests depend on build completing first
- Test results are not cached (always run fresh)
- Coverage outputs are tracked for caching
- Test environment variables are included as inputs
Best Practices
Write Testable Code
- Keep functions pure when possible
- Use dependency injection for external services
- Separate business logic from framework code
Test Coverage Goals
- Aim for 80%+ coverage on critical packages
- Focus on business logic over framework glue
- Test edge cases and error conditions
Fast Tests
- Mock external dependencies (databases, APIs)
- Use in-memory implementations for tests
- Keep unit tests under 100ms each
Continuous Integration
Tests run automatically on:- Pre-commit (via husky + lint-staged)
- Pull requests (via GitHub Actions)
- Before deployments
Troubleshooting
Tests Not Running
Ensure dependencies are installed:Vitest Not Found
Install Vitest at the root:Bun Test Errors
Ensure Bun is installed and up to date:Coverage Not Generated
Run with explicit coverage flag:Related Commands
| Command | Description |
|---|---|
pnpm test | Run all tests |
pnpm test:watch | Watch mode |
pnpm test:coverage | Generate coverage |
pnpm --filter <package> test | Test specific package |
bun test | Run Bun tests directly |
vitest run | Run Vitest tests directly |