Unit tests are the most granular tests that verify individual functions, components, or modules work correctly in isolation. These tests are fast, focused, and should be the foundation of our testing pyramid.
Location: src/__test__/unit/
Naming Convention: Unit test files should follow the pattern [module-name].test.[ext]
- For testing
src/lib/utils/formatting.ts, createsrc/__test__/unit/formatting.test.ts - For testing
src/ui/primitives/button.tsx, createsrc/__test__/unit/button.test.tsx - Group related tests in the same file when they test the same module
Run with:
- All unit tests:
bun test:unit - All tests:
bun test:run - Watch mode:
bun test:watch - Specific file:
bun test src/__test__/unit/formatting.test.ts
Examples:
src/__test__/unit/formatting.test.tsfor testing formatting utilitiessrc/__test__/unit/auth-utils.test.tsfor testing authentication utilitiessrc/__test__/unit/button.test.tsxfor testing button component
Benefits of Centralized Unit Tests:
- All unit tests in one location for easy discovery
- Clear separation between test types
- Consistent test organization
- Easy to run all unit tests at once
What to Unit Test:
- Pure utility functions (e.g., formatters, validators, parsers)
- Business logic functions
- React hooks
- Component rendering and behavior
- Data transformations
- Error handling edge cases
Integration tests verify that different parts of the application work together correctly, but they use mocks for external dependencies like databases and APIs. These tests are fast, reliable, and don't require external services to be running.
Location: src/__test__/integration/
Run with: bun test:integration
The integration tests use a dummy environment set in vitest.config.ts which provides all necessary environment variables with test values. This is sufficient for running integration tests without any additional setup.
Environment Validation:
- Integration tests run the
scripts:check-app-envscript before execution to ensure all required application environment variables are set. - This is configured in
package.json:"test:integration": "bun scripts:check-app-env && vitest run src/__test__/integration/"
Current Status: E2E tests are not yet implemented. There is a placeholder file at src/__test__/e2e/placeholder.test.ts.
Location: src/__test__/e2e/
Run with: bun test:e2e
Environment Validation:
- E2E tests run the
scripts:check-all-envscript before execution to ensure all required environment variables are set. - This is configured in
package.json:"test:e2e": "bun scripts:check-all-env && vitest run src/__test__/e2e/"
Development tests are specialized tests designed to assist with feature development by creating realistic scenarios for manual testing and observation in the dashboard.
Location: src/__test__/development/
Purpose: These tests spawn real resources (like sandboxes) that can be observed in the dashboard while developing features, eliminating the need to manually manage test resources.
Environment Setup:
To run development tests, you must create a .env.test file with the required environment variables:
# .env.test
TEST_E2B_DOMAIN=your-test-domain
TEST_E2B_API_KEY=your-test-api-key
TEST_E2B_TEMPLATE=base # optional, defaults to 'base'Note: Development tests interact with real E2B services and will create actual resources. Use with caution and ensure proper cleanup.
To run all tests (unit, integration, and E2E):
bun test:runTo run tests in watch mode (great for development):
bun test:watchTo run only unit tests:
bun test:unit
bun test src/__test__/unit/formatting.test.ts # specific unit test fileTo run only integration tests:
bun test:integrationTo run only E2E tests (currently just the placeholder):
bun test:e2eCurrently, unit and integration tests are configured to run in CI/CD:
- Unit tests are run from
src/__test__/unit/ - Integration tests are run from
src/__test__/integration/ - Both use the
.test.naming convention for automatic discovery
(See .github/workflows/test.yml)
Integration tests use the environment variables defined in .github/workflows/test.yml or .env files.
These values are sufficient for running integration tests without any additional setup.
When implementing E2E tests, you will need to:
- Create
.envfiles with real credentials for external services & additional variables for e2e tests - Update the
testEnvSchemainsrc/lib/env.tsto validate the required additional environment variables - Update the GitHub Actions workflow to include E2E tests with the necessary secrets
To start implementing E2E tests:
-
Create a proper environment setup:
- Create a
.env.test.examplefile showing required variables - Add validation in
scripts/check-e2e-env.tsto ensure all required variables are present
- Create a
-
Update GitHub Actions workflow:
- Add a new job for E2E tests in
.github/workflows/test.yml - Configure it to run after integration tests pass
- Add necessary secrets to GitHub repository settings
- Add a new job for E2E tests in
-
Implement actual E2E tests:
- Replace the placeholder in
src/__test__/e2e/placeholder.test.tswith real tests - Focus on critical user flows like authentication, team management, etc.
- Add logic to skip tests if required environment variables are missing
- Replace the placeholder in
-
Update this README:
- Document the new E2E tests and how to run them
- Update the environment variables section with the actual requirements
- Start with unit tests: Write unit tests for all utility functions, pure logic, and isolated components.
- Organize tests by type: Keep unit tests in
src/__test__/unit/, integration tests insrc/__test__/integration/. - Write integration tests for feature flows: Test how different parts work together, but mock external dependencies.
- Use E2E tests for critical paths: Focus on key user journeys like authentication, payment, etc.
- Keep E2E tests minimal: They're slower and more brittle, so use them sparingly.
- Use test data: Create and clean up test data in your E2E tests to avoid polluting your development/production environment.
- Skip E2E tests if environment variables are missing: This allows the tests to be run in environments without the necessary credentials.
- Follow the naming convention: Always use
.test.in the filename for all test files.