Skip to content

Commit e2a6b65

Browse files
Nolin NaidooNolin Naidoo
authored andcommitted
chore: add TESTING.md documentation
1 parent 406b1a4 commit e2a6b65

1 file changed

Lines changed: 218 additions & 0 deletions

File tree

docs/TESTING.md

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# Scrape-LE Testing Guidelines
2+
3+
This document outlines testing practices and policies for Scrape-LE development.
4+
5+
## Core Principle
6+
7+
**No broken or failed tests are allowed in commits.**
8+
9+
All tests must pass before code can be committed or merged. This ensures code quality and prevents regressions.
10+
11+
## Running Tests
12+
13+
### Run All Tests
14+
15+
```bash
16+
bun run test
17+
```
18+
19+
### Run Tests with Coverage
20+
21+
```bash
22+
bun run test:coverage
23+
```
24+
25+
### Run Tests in Watch Mode
26+
27+
```bash
28+
bun run test:watch
29+
```
30+
31+
### Run Tests for Specific File
32+
33+
```bash
34+
bun x vitest run src/detection/antiBot.test.ts
35+
```
36+
37+
## Test Structure
38+
39+
### Unit Tests
40+
41+
Located in `src/**/*.test.ts` and `src/**/*.spec.ts`:
42+
43+
- **Pure function tests** - Test detection logic in isolation
44+
- **Detection tests** - Test anti-bot, authentication, rate limit detection
45+
- **Utility tests** - Test helper functions
46+
- **Configuration tests** - Test config validation
47+
48+
### Integration Tests
49+
50+
Located in `src/**/*.spec.ts`:
51+
52+
- **Browser automation tests** - Test screenshot capture
53+
- **Cross-platform tests** - Ensure case-sensitivity compatibility
54+
- **End-to-end workflows** - Test complete check flows
55+
56+
## Test Coverage Requirements
57+
58+
- **Minimum Coverage**: Maintain reasonable coverage across core functionality
59+
- **Critical Paths**: All detection logic must be tested
60+
- **Error Handling**: All error paths must be covered
61+
- **Edge Cases**: Boundary conditions must be tested
62+
- **Security Tests**: Command injection, URL validation, SSRF prevention
63+
64+
## Before Committing
65+
66+
### Checklist
67+
68+
- [ ] All tests pass (`bun run test`)
69+
- [ ] No broken tests
70+
- [ ] No skipped tests (unless intentionally)
71+
- [ ] Type checking passes (`bun x tsc -p ./`)
72+
- [ ] Linting passes (`bun run lint`)
73+
74+
### CI/CD Validation
75+
76+
The CI pipeline automatically:
77+
78+
1. Runs all tests on Ubuntu, macOS, and Windows
79+
2. Generates coverage reports
80+
3. Verifies all tests pass
81+
4. Fails the build if any tests fail
82+
83+
## Fixing Failed Tests
84+
85+
### When a Test Fails
86+
87+
1. **Don't commit the failure** - Fix the test or the code
88+
2. **Run locally first** - Verify fix works before pushing
89+
3. **Check all platforms** - Ensure fix works on Linux/Windows (case sensitivity, etc.)
90+
4. **Update test if needed** - If behavior changed intentionally, update test
91+
92+
### Common Issues
93+
94+
- **Case sensitivity** - Use exact case for file references (`README.md` not `readme.md`)
95+
- **Mock issues** - Ensure mocks are properly reset in `beforeEach`
96+
- **Browser timing** - Account for browser launch delays in tests
97+
98+
## Test Best Practices
99+
100+
### 1. Use Descriptive Test Names
101+
102+
```typescript
103+
// ✅ Good
104+
it('should detect Cloudflare anti-bot protection', () => {
105+
// ...
106+
});
107+
108+
// ❌ Bad
109+
it('works', () => {
110+
// ...
111+
});
112+
```
113+
114+
### 2. Test One Thing Per Test
115+
116+
```typescript
117+
// ✅ Good - separate tests
118+
it('should detect Cloudflare', () => { /* ... */ });
119+
it('should detect reCAPTCHA', () => { /* ... */ });
120+
121+
// ❌ Bad - multiple concerns
122+
it('should detect Cloudflare and reCAPTCHA', () => { /* ... */ });
123+
```
124+
125+
### 3. Use Arrange-Act-Assert Pattern
126+
127+
```typescript
128+
it('should validate URL before checking', () => {
129+
// Arrange
130+
const url = 'https://example.com';
131+
132+
// Act
133+
const result = validateUrl(url);
134+
135+
// Assert
136+
expect(result.valid).toBe(true);
137+
});
138+
```
139+
140+
### 4. Clean Up Mocks
141+
142+
```typescript
143+
beforeEach(() => {
144+
vi.clearAllMocks();
145+
// Reset mocks to default state
146+
});
147+
```
148+
149+
## Cross-Platform Testing
150+
151+
### Case Sensitivity
152+
153+
Always use exact case for file references:
154+
155+
```typescript
156+
// ✅ Good - works on all platforms
157+
const content = readSampleFile('README.md');
158+
159+
// ❌ Bad - fails on Linux
160+
const content = readSampleFile('readme.md');
161+
```
162+
163+
### Path Separators
164+
165+
Use platform-agnostic path handling:
166+
167+
```typescript
168+
import { join } from 'path';
169+
const filePath = join(SAMPLE_DIR, filename);
170+
```
171+
172+
## Coverage Reports
173+
174+
Coverage reports are generated automatically:
175+
176+
- **Location**: `coverage/index.html`
177+
- **Format**: HTML, LCOV, JSON
178+
- **CI/CD**: Coverage uploaded as artifact
179+
180+
## Continuous Integration
181+
182+
### GitHub Actions
183+
184+
Tests run automatically on:
185+
186+
- **Ubuntu** (latest)
187+
- **macOS** (latest)
188+
- **Windows** (latest)
189+
190+
All platforms must pass for the build to succeed.
191+
192+
### Pre-commit Hooks
193+
194+
Consider setting up pre-commit hooks to run tests before commits:
195+
196+
```bash
197+
# Install husky (if needed)
198+
bun add -d husky
199+
200+
# Add pre-commit hook
201+
echo "bun run test" > .husky/pre-commit
202+
```
203+
204+
## Reporting Test Issues
205+
206+
If you encounter test failures:
207+
208+
1. **Run locally** - Verify it fails consistently
209+
2. **Check CI logs** - See platform-specific errors
210+
3. **Reproduce** - Document steps to reproduce
211+
4. **Fix or report** - Either fix or create an issue
212+
213+
## Related Documentation
214+
215+
- [Performance Monitoring](PERFORMANCE.md) - Performance testing and benchmarks
216+
- [Architecture](ARCHITECTURE.md) - Code structure
217+
- [Commands](COMMANDS.md) - Command testing guidelines
218+

0 commit comments

Comments
 (0)