managarten/docs/TESTING_QUICK_REFERENCE.md
Wuesteon 304897261d test: implement comprehensive automated testing system with daily CI/CD
Implement rock-solid automated testing infrastructure for mana-core-auth
with daily execution, notifications, and comprehensive monitoring.

Test Suite Improvements:
- Fix all 36 failing BetterAuthService tests (missing service mocks)
- Add 21 JwtAuthGuard tests achieving 100% statement coverage
- Create silentError helper to suppress intentional error logs
- Fix Todo backend TaskService test structure
- Add jose mock for JWT testing
- Configure jest collectCoverageFrom for mana-core-auth

GitHub Actions Workflow:
- Daily automated test execution (2 AM UTC + manual trigger)
- Matrix parallelization across 6 backend services
- PostgreSQL and Redis service containers
- Coverage enforcement (80% threshold)
- Multi-channel notifications (Discord, Slack, GitHub Issues)
- Support for success notifications (opt-in)

Test Infrastructure:
- Coverage aggregation across multiple services
- Flaky test detection with 30-run history tracking
- Performance metrics tracking with regression detection
- Test data seeding and cleanup scripts
- Comprehensive test reporting with formatted metrics

Documentation:
- TESTING_GUIDE.md (4000+ words) - Complete testing documentation
- AUTOMATED_TESTING_SYSTEM.md - System architecture and workflows
- DISCORD_NOTIFICATIONS_SETUP.md - Discord webhook setup guide
- TESTING_DEPLOYMENT_CHECKLIST.md - Pre-deployment verification
- TESTING_QUICK_REFERENCE.md - Quick command reference

Final Result:
- 180/180 tests passing (100% pass rate)
- Zero console errors in test output
- Automated daily testing with rich notifications
- Production-ready test infrastructure
2025-12-25 19:12:27 +01:00

245 lines
4.4 KiB
Markdown

# Testing Quick Reference
Fast reference guide for common testing tasks in the ManaCore monorepo.
## Quick Commands
### Run Tests
```bash
# All tests
pnpm test
# Specific service
cd services/mana-core-auth && pnpm test
# With coverage
pnpm test:cov
# Watch mode
pnpm test:watch
# Specific file
pnpm test src/auth/auth.service.spec.ts
```
### Run Tests with Script
```bash
# All packages
./scripts/run-tests-with-coverage.sh
# Specific package
./scripts/run-tests-with-coverage.sh mana-core-auth
./scripts/run-tests-with-coverage.sh chat-backend
```
### Setup/Cleanup
```bash
# Start Docker services
pnpm docker:up
# Seed test data
./scripts/test-data/seed-test-data.sh
# Clean test data
./scripts/test-data/cleanup-test-data.sh
# Stop Docker
pnpm docker:down
```
## Test Patterns
### Unit Test Template (Backend)
```typescript
import { Test } from '@nestjs/testing';
import { MyService } from './my.service';
describe('MyService', () => {
let service: MyService;
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [MyService],
}).compile();
service = module.get<MyService>(MyService);
});
it('should do something', () => {
const result = service.doSomething();
expect(result).toBe(expected);
});
});
```
### Integration Test Template
```typescript
describe('Integration Test', () => {
let app: INestApplication;
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = module.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('should complete flow', async () => {
// Test full flow
});
});
```
### Mock Template
```typescript
// Mock entire module
jest.mock('./external-service', () => ({
ExternalService: jest.fn().mockImplementation(() => ({
method: jest.fn().mockResolvedValue(mockData),
})),
}));
// Mock specific function
jest.spyOn(service, 'method').mockResolvedValue(mockData);
```
## Coverage
### View Coverage
```bash
# Generate report
pnpm test:cov
# Open HTML report (macOS)
open coverage/lcov-report/index.html
# Open HTML report (Linux)
xdg-open coverage/lcov-report/index.html
```
### Coverage Thresholds
- **Global**: 80% minimum
- **Critical paths**: 100% required
- **Check in CI**: Automated daily tests
## Test Data
### Pre-seeded Users
| Email | Password | Role |
|-------|----------|------|
| `test-user-1@example.com` | `TestPassword123!` | user |
| `test-user-2@example.com` | `TestPassword123!` | user |
| `admin@example.com` | `AdminPassword123!` | admin |
### Create Test User
```typescript
const testUser = {
id: uuid(),
email: `test-${Date.now()}@example.com`,
name: 'Test User',
role: 'user',
};
```
## Troubleshooting
### Database Connection Failed
```bash
# 1. Start Docker
pnpm docker:up
# 2. Verify running
docker ps | grep postgres
# 3. Test connection
psql -U manacore -h localhost -p 5432 -d manacore
```
### Tests Fail in CI but Pass Locally
1. Check environment variables
2. Verify database setup in workflow
3. Check for hardcoded paths
4. Review Docker service health checks
### Flaky Tests
1. Ensure proper `await` usage
2. Check test isolation
3. Mock time-dependent functions
4. Add explicit waits
```typescript
// ❌ Flaky
it('should complete', () => {
asyncOperation();
expect(result).toBeDefined();
});
// ✅ Stable
it('should complete', async () => {
await asyncOperation();
expect(result).toBeDefined();
});
```
## CI/CD
### Trigger Daily Tests Manually
1. Go to GitHub Actions
2. Select "Daily Tests" workflow
3. Click "Run workflow"
4. Set optional parameters
5. Run
### View Test Results
- **Workflow Runs**: GitHub Actions tab
- **Coverage**: Download artifacts from workflow
- **Metrics**: Check test-metrics artifact
- **Flaky Tests**: Check flaky-test-report artifact
## Best Practices
### DO ✅
- Write tests for new features
- Use descriptive names
- Keep tests isolated
- Mock external services
- Run locally before push
- Maintain 80%+ coverage
### DON'T ❌
- Skip tests
- Use vague names
- Depend on test order
- Make real API calls
- Hardcode values
- Commit failing tests
## Getting Help
- **Docs**: `/Users/wuesteon/dev/mana_universe/manacore-monorepo/docs/TESTING_GUIDE.md`
- **Examples**: `/Users/wuesteon/dev/mana_universe/manacore-monorepo/docs/test-examples/`
- **Issues**: Label with `testing` on GitHub
- **Team**: Ask in #testing Slack channel