134 lines
2.4 KiB
Markdown
134 lines
2.4 KiB
Markdown
# Testing Guide
|
|
|
|
## Setup
|
|
|
|
Tests verwenden **Jest** und **Supertest**.
|
|
|
|
### Installation
|
|
```bash
|
|
npm install # Installiert auch dev-dependencies
|
|
```
|
|
|
|
## Tests ausführen
|
|
|
|
### Alle Tests
|
|
```bash
|
|
npm test
|
|
```
|
|
|
|
### Tests mit Watch-Mode (Development)
|
|
```bash
|
|
npm run test:watch
|
|
```
|
|
|
|
### Nur Unit-Tests
|
|
```bash
|
|
npm run test:unit
|
|
```
|
|
|
|
### Mit Coverage-Report
|
|
```bash
|
|
npm test # Coverage ist standardmäßig aktiviert
|
|
```
|
|
|
|
Coverage-Report wird in `coverage/` erstellt.
|
|
|
|
## Test-Struktur
|
|
|
|
```
|
|
backend/
|
|
├── controllers/
|
|
│ ├── __tests__/
|
|
│ │ └── authController.test.js
|
|
│ └── authController.js
|
|
├── middleware/
|
|
│ ├── __tests__/
|
|
│ │ └── rateLimiter.test.js
|
|
│ └── rateLimiter.js
|
|
└── config/
|
|
├── __tests__/
|
|
│ └── env.test.js
|
|
└── env.js
|
|
```
|
|
|
|
## Test-Beispiele
|
|
|
|
### Unit Test (Controller)
|
|
```javascript
|
|
describe('Auth Controller', () => {
|
|
it('should return 400 if username is missing', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/login')
|
|
.send({ password: 'test123' });
|
|
|
|
expect(response.status).toBe(400);
|
|
});
|
|
});
|
|
```
|
|
|
|
### Integration Test (mit MongoDB)
|
|
```javascript
|
|
beforeAll(async () => {
|
|
await mongoose.connect(process.env.TEST_MONGO_URI);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await mongoose.connection.close();
|
|
});
|
|
```
|
|
|
|
## Eigene Tests schreiben
|
|
|
|
1. **Test-Datei erstellen:**
|
|
```bash
|
|
# Immer in __tests__/ Ordner
|
|
touch controllers/__tests__/userController.test.js
|
|
```
|
|
|
|
2. **Test schreiben:**
|
|
```javascript
|
|
const request = require('supertest');
|
|
|
|
describe('User Controller', () => {
|
|
it('should get all users', async () => {
|
|
// Test implementation
|
|
});
|
|
});
|
|
```
|
|
|
|
3. **Test ausführen:**
|
|
```bash
|
|
npm test
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
- ✅ Ein Test = eine Funktion/Feature
|
|
- ✅ Tests sollten unabhängig voneinander sein
|
|
- ✅ Mocks für externe Services verwenden
|
|
- ✅ Aussagekräftige Test-Namen
|
|
- ✅ Arrange-Act-Assert Pattern
|
|
|
|
## CI/CD Integration
|
|
|
|
Tests werden automatisch ausgeführt bei:
|
|
- Pull Requests
|
|
- Vor Deployment
|
|
- In GitHub Actions (wenn konfiguriert)
|
|
|
|
## Aktuelle Test-Coverage
|
|
|
|
Führe aus: `npm test`
|
|
|
|
Ziele:
|
|
- [ ] 80%+ Coverage für Controllers
|
|
- [ ] 80%+ Coverage für Middleware
|
|
- [ ] 90%+ Coverage für Utils
|
|
|
|
## Nächste Schritte
|
|
|
|
1. [ ] Tests für alle Controller schreiben
|
|
2. [ ] Integration-Tests mit Test-DB
|
|
3. [ ] E2E-Tests für kritische Flows
|
|
4. [ ] CI/CD Pipeline einrichten
|