This file contains critical documentation about testing in WayrApp, including setup,
best practices, and troubleshooting information.
Author
Exequiel Trujillo
Since
1.0.0
Example
// Quick setup for testing // 1. Create test database // 2. Configure .env.test // 3. Run: npm run test:db:setup // 4. Run: npm run test:integration:safe
Testing Guide
This document provides information about testing in WayrApp, including setup,
best practices, and troubleshooting.
๐ก๏ธ Test Database Safety
CRITICAL: WayrApp uses a separate test database to prevent production data loss.
Tests perform complete database cleanup after each test, which would
delete all production data if run against the production database.
ARCHITECTURAL DECISION: ALL tests (unit and integration) require a test database
configuration via .env.test. This is intentional to enforce security and prevent
accidental production data loss across the entire development team.
Safety Measures
โ Mandatory test database - ALL tests require .env.test configuration
โ Automatic validation prevents accidental use of production database
โ Safe test commands that setup test database before running tests
โ Database isolation with complete cleanup after each test
โ Team consistency - All developers must follow the same setup process
๐ Quick Setup
REQUIRED FOR ALL TESTS - You cannot run any tests without this setup.
1. Create Test Database
Create a separate database for testing (never use your production database):
# Copy example configuration cp.env.example.env.test # Edit .env.test with your test database URL # IMPORTANT: Use a DIFFERENT database than production
Problem: ALL tests fail with database configuration error.
Cause: This is intentional - our architectural decision requires test database for all tests.
Solution:
# Check configuration npmruntest:db:check
# Create .env.test file (REQUIRED for all tests) cp.env.example.env.test # Edit .env.test with test database URL
# Setup test database npmruntest:db:setup
2. "Test and production databases are the same" Error
Problem: Safety check prevents using production database for tests.
Solution:
Create a separate test database
Update .env.test with the test database URL
Ensure the test database URL is different from production
3. Database Connection Errors
Problem: Tests fail to connect to test database.
Solution:
Verify test database exists and is accessible
Check connection string format in .env.test
Ensure database permissions are correct
4. Schema Sync Issues
Problem: Tests fail due to outdated database schema.
Solution:
# Reset and update test database schema npmruntest:db:setup
5. Rate Limiting in Tests
Problem: Authentication tests fail due to rate limiting.
Solution: Tests include automatic rate limit handling with retries and graceful skipping.
Debug Mode
Run tests with verbose output:
# Verbose test output npmruntest:integration----verbose
# Debug specific test file npmruntest:integration----testNamePattern="Authentication"
Testing Guide for WayrApp
This file contains critical documentation about testing in WayrApp, including setup, best practices, and troubleshooting information.
Author
Exequiel Trujillo
Since
1.0.0
Example
Testing Guide
This document provides information about testing in WayrApp, including setup, best practices, and troubleshooting.
๐ก๏ธ Test Database Safety
CRITICAL: WayrApp uses a separate test database to prevent production data loss. Tests perform complete database cleanup after each test, which would delete all production data if run against the production database.
ARCHITECTURAL DECISION: ALL tests (unit and integration) require a test database configuration via
.env.test
. This is intentional to enforce security and prevent accidental production data loss across the entire development team.Safety Measures
.env.test
configuration๐ Quick Setup
REQUIRED FOR ALL TESTS - You cannot run any tests without this setup.
1. Create Test Database
Create a separate database for testing (never use your production database):
Option A: Using Neon (Recommended)
Option B: Local PostgreSQL
2. Configure Test Environment
Example
.env.test
:3. Verify Configuration
4. Setup Test Database
๐งช Running Tests
Safe Commands (Recommended)
Individual Test Commands
NOTE: All commands require test database configuration via
.env.test
Watch Mode
NOTE: Requires test database configuration via
.env.test
๐ Test Structure
Test Types
IMPORTANT: All test types require test database configuration for security.
Unit Tests (
.test.ts
)Integration Tests (
.integration.test.ts
)Test Organization
๐ง Test Configuration
Jest Configuration
jest.config.js
jest.integration.config.js
Key Configuration Features
NODE_ENV=test
๐ญ Test Data Factories
Use factories to create consistent test data:
๐ ๏ธ Writing Tests
Unit Test Example
Integration Test Example
๐จ Troubleshooting
Common Issues
1. "No test database configured" Error
Problem: ALL tests fail with database configuration error. Cause: This is intentional - our architectural decision requires test database for all tests. Solution:
2. "Test and production databases are the same" Error
Problem: Safety check prevents using production database for tests. Solution:
.env.test
with the test database URL3. Database Connection Errors
Problem: Tests fail to connect to test database. Solution:
.env.test
4. Schema Sync Issues
Problem: Tests fail due to outdated database schema. Solution:
5. Rate Limiting in Tests
Problem: Authentication tests fail due to rate limiting. Solution: Tests include automatic rate limit handling with retries and graceful skipping.
Debug Mode
Run tests with verbose output:
๐ Test Coverage
Generate test coverage reports:
๐ Continuous Integration
Tests run automatically in CI/CD pipelines:
๐ Best Practices
Test Writing
afterEach
hooksDatabase Testing
Performance
beforeAll
/afterAll
for expensive setup/teardown๐ Related Documentation